props.history is undefined after upgrading React Router
TypeError: Cannot read properties of undefined (reading 'push')Short answer
From React Router 6 on, a route does not pass history, location or match props to its component. history itself is gone. Read router data with hooks, call useNavigate for navigation, and wrap class components in a small function component that passes the values down as props.
Fix it fast
1Find out which cause you have
Check which version is installed and how the route is declared. A route that uses component= or render= is still v5 syntax.
npm ls react-router-dom grep -rn "component=\|withRouter\|useHistory" src/2Match what you see, then jump to its fix
- 1
Routes use <Route component={Page} /> and the page reads this.props.historyThe route uses component or render instead of elementFix - 2
Cannot read properties of undefined (reading 'push')history.push was replaced by useNavigateFix - 3
withRouter is not exported, or this.props.history is undefined in a classA class component was wrapped with withRouter, which no longer existsFix
- 1
What does the error look like?
TypeError: Cannot read properties of undefined (reading 'push')TypeError: _this.props.history is undefined
Cause 1 of 3
The route uses component or render instead of element
1Check
In v6 the route renders the element you give it and nothing is added to its props.
grep -rn "<Route" src/2Fix
Use Routes and element. If the page needs navigation, read it inside the page with a hook.
<Routes>
<Route path="/orders" element={<Orders />} />
</Routes>3Verify
The page renders; props.history is no longer used.
grep -rn "props.history" src/Confirmed by reactrouter.com
Cause 2 of 3
history.push was replaced by useNavigate
1Check
The upgrade guide maps useHistory and props.history to useNavigate, and navigate(to, { replace: true }) replaces instead of pushing.
grep -rnE "history\.(push|replace)" src/2Fix
Call useNavigate in a function component.
import { useNavigate } from 'react-router-dom';
function SaveButton() {
const navigate = useNavigate();
return <button onClick={() => navigate('/orders')}>Save</button>;
}3Verify
Click the button; the URL changes.
console.log(window.location.pathname);Confirmed by reactrouter.com
Cause 3 of 3
A class component was wrapped with withRouter, which no longer exists
1Check
The upgrade guide says to replace withRouter with hooks. Hooks cannot be called inside a class.
grep -rn "withRouter" src/2Fix
Write a small function component that reads the hooks and passes the values as props.
import { useNavigate, useLocation, useParams } from 'react-router-dom';
export function withRouter(Component) {
return function Wrapped(props) {
const navigate = useNavigate();
const location = useLocation();
const params = useParams();
return <Component {...props} navigate={navigate} location={location} params={params} />;
};
}
// in the class: this.props.navigate('/orders')3Verify
The class calls this.props.navigate without errors.
grep -rn "this.props.history" src/Confirmed by reactrouter.com
How do I stop it coming back?
- Follow the official v5 to v6 upgrade guide in one pass instead of fixing errors one at a time.
- Search for useHistory, withRouter, Switch and component= before you merge.
- Move class components to function components when you next touch them.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript: history.push does not exist after upgrading React Router
Property 'push' does not exist on type 'History'
The History type TypeScript knows is the browser's window.history, which has pushState but no push method.
2 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix