React Router with MUI Tabs: Step-by-Step Guide
In this article
This guide shows you how to combine React Router with Material-UI (MUI) Tabs so that each tab has its own URL path. This is useful for navigation in single-page apps—users can bookmark, share, and use the browser back/forward buttons. You'll build a tabbed interface where clicking a tab updates the URL and the displayed content, and refreshing the page keeps the correct tab active.
What You'll Need
- A React project (created with Create React App, Vite, or similar)
- React Router DOM installed (
npm install react-router-dom) - MUI installed (
npm install @mui/material @emotion/react @emotion/styled) - Basic familiarity with React components and hooks
Step-by-Step
- Set up your router structure. Wrap your app in a
BrowserRouter(usually inindex.jsormain.jsx). Then define routes for each tab. For example, if you have tabs for Home, Profile, and Settings, your routes might be/,/profile, and/settings. - Create a layout component that includes the Tabs. This component will render the
TabsandTabcomponents from MUI, and also render the matched route content viaOutlet(if using nested routes) or by directly rendering the route components. - Use
useLocationto get the current path. In the layout component, callconst location = useLocation();to know which tab is active based on the URL. - Set the
valueprop ofTabsto the current path. For example, if your tab values are the route paths themselves, setvalue={location.pathname}. This ensures the correct tab is highlighted when the URL changes (including on refresh). - Handle tab changes with
onChange. UseuseNavigatefrom React Router to navigate to the new path when a tab is clicked. Example:const navigate = useNavigate();and then inonChange, callnavigate(newValue). - Define your tab content. Create separate components for each tab's content (e.g.,
HomePage,ProfilePage,SettingsPage). - Set up routes. In your router configuration, define a route that renders the layout component, and inside it, define nested routes for each tab. For example:
<Routes> <Route element={<TabLayout />}> <Route path="/" element={<HomePage />} /> <Route path="/profile" element={<ProfilePage />} /> <Route path="/settings" element={<SettingsPage />} /> </Route> </Routes> - Render the
Outletin the layout. InsideTabLayout, after theTabscomponent, include<Outlet />to render the matched child route. - Test the behavior. Click each tab and verify the URL changes, the content updates, and the active tab stays highlighted when you refresh the page.
Common Mistakes
- Using
valueas an index instead of a path. MUI Tabs can use numeric indices, but for URL synchronization, it's easier to use the path string as the value. If you use indices, you must map them to paths manually. - Forgetting to wrap the app in
BrowserRouter. Without it,useNavigateanduseLocationwill throw errors. - Not handling the initial route correctly. If the user visits
/profiledirectly, the Tabs must reflect that. Usinglocation.pathnameas the value handles this automatically. - Using
Linkwithoutvaluesync. If you useLinkcomponents inside the Tabs, the active state may not update correctly unless you also control thevalueprop.
Tips
- Use a constant for tab paths. Define an array of objects with
labelandpathto avoid typos and make the code cleaner. - Consider using
useEffectfor redirects. If a user visits a path that doesn't match any tab, you can redirect to the first tab usingNavigatein a catch-all route. - Use
variant="scrollable"for many tabs. On small screens, this prevents overflow. - Keep the tab content lazy-loaded. Use
React.lazyandSuspenseto split code per tab for better performance. - Set
indicatorColorandtextColorto match your theme. MUI defaults are fine, but you can customize.
Troubleshooting
- The tab doesn't highlight on refresh. Make sure you are using
location.pathnameas thevalueprop and that the path exactly matches the route path (watch for trailing slashes). - Clicking a tab does nothing. Check that
onChangeis correctly callingnavigatewith the new value. Also ensure the route exists. - The URL changes but content doesn't. Verify that your
Routesare set up correctly and that theOutletis placed inside the layout component. - Error: "useNavigate() may be used only in the context of a Router" This means
BrowserRouteris missing or not wrapping the component that uses the hook. - Active tab is off by one. If you use numeric indices, ensure the index matches the route order. Prefer using path strings to avoid this.
Frequently Asked Questions
Can I use MUI Tabs with React Router without nested routes?
Yes, you can. Instead of using Outlet, you can render the content conditionally based on location.pathname, but nested routes are cleaner and more scalable.
How do I set a default tab when the URL is empty?
Set your home route path to "/" and make it the first tab. In the Tabs, the value will be "/" when the URL is the root.
What if I want to use tab indices instead of paths?
You can, but you need to map the index to the path. For example, store an array of paths and use paths[index] in onChange to navigate.
How do I handle deep linking to a tab?
Deep linking works automatically because each tab has its own URL. When the app loads, useLocation reads the path and sets the correct tab.
Can I use query parameters with tabs?
Yes, you can include query params in the path or use useSearchParams to read them. The Tabs value would still be the pathname, not the query string.
Is it possible to have tabs inside a nested layout?
Absolutely. You can nest layouts by having a parent route with an Outlet that renders a child layout containing the Tabs.