13

I am using react-router 2.4.0 and want to link to another route programmatically (what I did before using <Link>).

It's explained nicely in this SO post where they say in 2.4.x you should use the decorator pattern with withRouter, so I am using the following code:

import {withRouter} from 'react-router' // further imports omitted


class CreateJobItemFormRaw extends React.Component {
  ...
}

const CreateJobItemForm = withRouter(CreateJobItemFormRaw)
export default CreateJobItemForm

Then in other files, I use

import CreateJobItemForm from './CreateJobItemForm'

However, with this approach my app doesn't render at all any more and the console outputs:

CreateJobItemForm.js:76 Uncaught TypeError: (0 , _reactRouter.withRouter) is not a function

Can anyone help me solve this?

Community
  • 1
  • 1
nburk
  • 22,409
  • 18
  • 87
  • 132

4 Answers4

7

I trust that you are in fact using react-router 2.4.0, but in my case it was worth double-checking that my package.json did in fact enforce that version. I modified my package.json as such:

"dependencies": {
  "react-router": "^2.4.0",
  ...
}

Hope this helps.

codepringle
  • 343
  • 2
  • 14
  • Works for me, thx ! The `npm update` command wasn't enough, I had to uninstall react-router then make an `npm install react-router@2.8.1` to make it update. – Julha Sep 18 '16 at 01:41
4

In my case, I upgraded to react-router v6 and discovered that withRouter was removed and hooks should be used instead.

Upgrade documentation here: https://reactrouter.com/en/main/upgrading/v5

Along with the upgrade to v5.1, you should replace any usage of withRouter with hooks.

Here is a detailed guide to hooks: https://reacttraining.com/blog/react-router-v5-1/

Drew Daniels
  • 314
  • 4
  • 16
datchung
  • 3,778
  • 1
  • 28
  • 29
0

In a comment to another answer you linked to this question and said that you're trying to navigate using react-router 2.4+. Try and put the PropType specifications in the file and see if that gives you any warnings. For Example:

// PropTypes
Example.propTypes = {
  router: React.PropTypes.shape({
    push: React.PropTypes.func.isRequired
  }).isRequired
};
Guy
  • 65,082
  • 97
  • 254
  • 325
  • thanks for the answer! I did put in the `propTypes`, but the issue remains. the weird thing is that my whole app doesn't at all, even though initially the component that uses `withRouter` isn't even mounted... – nburk May 24 '16 at 16:11
  • 1
    Is there any chance that the bundle that's being served to the browser is out-of-date. i.e. the wrong one. I saw this sort of thing once where webpack was talking a long time to rebuild the bundle.js file and I had refreshed the browser and it picked up an old one. You could put a `debugger;` statement in there and check what has been put onto `this.props` by react-router and see if it looks reasonable to you... – Guy May 25 '16 at 20:36
  • yay guys, if anyone gets this far, don't forget to restart webpack or rebundle :P it made it for me – mezod Jun 07 '16 at 19:29
-1
import { withRouter } from 'react-router-dom'

react-router v4.x
Julian
  • 33,915
  • 22
  • 119
  • 174
Liu Joe
  • 11