1

I'm relatively new to Typescript and React.

Following tutorials, I'm trying to add withRouter from "react-router-dom", but I'm getting an error saying Module '"react-router-dom"' has no exported member 'withRouter'.

There is no reference to withRouter anywhere in the react-router-dom node-modules package. This is confusing to me and I don't know if typescript is the origin of the missing function. I know they have removed some functionality but I can't find anything that could replace it, in order to access this.props.history.push() from inside my component.

Can anyone advise on this? What am I doing wrong?

Maxime Franchot
  • 1,015
  • 1
  • 10
  • 24
  • Your `react-router-dom` version? – Muhammed Rahif Jan 09 '22 at 19:29
  • 1
    The duplicate explains what happened to `withRouter`. To address the history issue you'll need to convert class components to function components so you can use the `useNavigate` hook, or create a custom `withRouter` HOC replacement and pass in the `navigate` function which replaced the `history` object. https://stackoverflow.com/questions/69899955/problem-in-redirecting-programmatically-to-a-route-in-react-router-v6/69902006#69902006 – Drew Reese Jan 09 '22 at 21:44

1 Answers1

2

what version of react-router-dom are you using? looks like withRouter isn't supported anymore. For v6 it has been replaced with hooks. See here for the explanation from their docs, there is also a bit here

rupson
  • 41
  • 2