ReactReact RouterError Fix2026 Updated
React Router v6 Not Working— Fixed [2026]
Switch → Routes · useHistory → useNavigate · Redirect → Navigate · Outlet
January 20266 min readReact Router v6 · React 18
⚡ v5 → v6 Quick Reference
| v5 (remove) | v6 (use this) |
|---|---|
| <Switch> | <Routes> |
| <Route component={C}> | <Route element={<C />}> |
| useHistory() | useNavigate() |
| <Redirect to='/x'> | <Navigate to='/x' replace /> |
| exact prop | not needed — all routes exact by default |
| history.push('/x') | navigate('/x') |
| history.goBack() | navigate(-1) |
v5 → v6 Breaking Changes Overview
Error
ERROR: 'Switch' is not exported from 'react-router-dom'
ERROR: useHistory is not a function
ERROR: 'Redirect' is not exported from 'react-router-dom'React Router v6 is a near-complete rewrite. It fixes API inconsistencies from v5 but requires updating most of your routing code if you are upgrading. Here are all the fixes in order of how commonly they are encountered.
11 min
Switch → Routes
'Switch' is not exported from react-router-domApp.jsx
// ❌ v5
import { BrowserRouter, Switch, Route } from 'react-router-dom'
function App() {
return (
<BrowserRouter>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
</Switch>
</BrowserRouter>
)
}
// ✅ v6
import { BrowserRouter, Routes, Route } from 'react-router-dom'
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} /> {/* exact by default */}
<Route path="/about" element={<About />} />
<Route path="*" element={<NotFound />} /> {/* 404 catch-all */}
</Routes>
</BrowserRouter>
)
}21 min
Route element Prop — Not component
Route not rendering — wrong prop nameApp.jsx
// ❌ v5 — all these patterns removed in v6
<Route path="/home" component={Home} />
<Route path="/home" render={() => <Home extra="prop" />} />
<Route path="/home" children={<Home />} />
// ✅ v6 — always use element with JSX
<Route path="/home" element={<Home />} />
<Route path="/home" element={<Home extra="prop" />} /> {/* props inline */}
<Route path="/users/:id" element={<UserPage />} /> {/* params via useParams() */}32 min
useHistory → useNavigate
useHistory is not a functioncomponents/LoginForm.jsx
// ❌ v5
import { useHistory } from 'react-router-dom'
const history = useHistory()
history.push('/dashboard')
history.replace('/login')
history.goBack()
history.push('/page', { fromLogin: true }) // with state
// ✅ v6
import { useNavigate } from 'react-router-dom'
const navigate = useNavigate()
navigate('/dashboard') // push
navigate('/login', { replace: true }) // replace
navigate(-1) // go back
navigate(1) // go forward
navigate('/page', { state: { fromLogin: true } }) // with state
// Read state at destination
import { useLocation } from 'react-router-dom'
const location = useLocation()
const fromLogin = location.state?.fromLogin41 min
Redirect → Navigate
'Redirect' is not exported from react-router-domApp.jsx
// ❌ v5
import { Redirect } from 'react-router-dom'
<Route path="/old" render={() => <Redirect to="/new" />} />
// ✅ v6
import { Navigate } from 'react-router-dom'
<Route path="/old" element={<Navigate to="/new" replace />} />
// ✅ Programmatic redirect (after form submit etc.)
navigate('/success', { replace: true })53 min
Nested Routes With Outlet
Child routes not rendering in layoutApp.jsx + layouts/DashboardLayout.jsx
import { Routes, Route, Outlet, Link } from 'react-router-dom'
// Layout component — renders Outlet where children go
function DashboardLayout() {
return (
<div>
<nav>
<Link to="overview">Overview</Link>
<Link to="settings">Settings</Link>
</nav>
<main>
<Outlet /> {/* child routes render here */}
</main>
</div>
)
}
// App — nested route structure
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
{/* Nested routes */}
<Route path="/dashboard" element={<DashboardLayout />}>
<Route index element={<Overview />} /> {/* /dashboard */}
<Route path="settings" element={<Settings />} /> {/* /dashboard/settings */}
<Route path="users/:id" element={<UserDetail />} />{/* /dashboard/users/42 */}
</Route>
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
)
}62 min
Protected Route Pattern in v6
Auth guard — redirect if not logged incomponents/ProtectedRoute.jsx
import { Navigate, useLocation } from 'react-router-dom'
import { useAuth } from '../context/AuthContext'
export function ProtectedRoute({ children }) {
const { user } = useAuth()
const location = useLocation()
if (!user) {
// Redirect to login, preserve intended destination
return <Navigate to="/login" state={{ from: location }} replace />
}
return children
}
// Usage in App.jsx
<Route path="/dashboard" element={
<ProtectedRoute>
<Dashboard />
</ProtectedRoute>
} />
// In LoginPage — redirect back after login
const navigate = useNavigate()
const location = useLocation()
const from = location.state?.from?.pathname ?? '/dashboard'
// After successful login:
navigate(from, { replace: true })Prevention
- Check react-router-dom version: npm ls react-router-dom
- Replace all at once: Switch→Routes, component→element, useHistory→useNavigate, Redirect→Navigate
- Remove all exact props — routes are exact by default in v6
- Use Outlet in layout components for nested route content
- Follow the official React Router v5 to v6 migration guide for larger codebases
Frequently Asked Questions
What are the main breaking changes in React Router v6?+−
Switch is now Routes. The Route component prop is now element (JSX). useHistory is now useNavigate. Redirect is now Navigate component. The exact prop is removed — all routes are exact by default. Nested routes use Outlet instead of rendering children inside page components.
How do I fix 'Switch is not exported from react-router-dom'?+−
Replace <Switch> with <Routes>. Also update <Route component={Page}> to <Route element={<Page />}>. The entire Routes/Route API changed in v6.
useHistory is not a function in React Router v6?+−
useHistory was removed. Replace with useNavigate: const navigate = useNavigate(). Then: navigate('/path') to push, navigate('/path', { replace: true }) to replace, navigate(-1) to go back.
How do nested routes work in React Router v6?+−
In v6, nested routes use the Outlet component. Define child routes inside the parent Route in JSX. The parent layout component renders <Outlet /> where child content should appear.
How do I redirect in React Router v6?+−
Use the Navigate component: <Route path='/old' element={<Navigate to='/new' replace />} />. Or programmatically: const navigate = useNavigate(); navigate('/new', { replace: true }).
React Router v6 route not matching — why?+−
In v6, all routes are exact by default — you no longer need the exact prop. Also check that your Routes component contains all the Route definitions, and that the BrowserRouter wraps your entire app including the Routes.
Need Expert Help?
We Build Production-Grade React Apps
Softplix builds full-stack React and Next.js applications. Need help migrating to React Router v6 or setting up routing architecture? We can help.
Talk to an Engineer