Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

props.history is undefined after upgrading React Router

ErrorTypeError: 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.

  • Applies to React Router 6.30, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1Routes use <Route component={Page} /> and the page reads this.props.historyThe route uses component or render instead of elementFix
    2. 2Cannot read properties of undefined (reading 'push')history.push was replaced by useNavigateFix
    3. 3withRouter is not exported, or this.props.history is undefined in a classA class component was wrapped with withRouter, which no longer existsFix

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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.