javascript - Uncaught TypeError: (0 , _reactRouter.withRouter) is not a function when navigating to route programmatically in re

I am using react-router 2.4.0 and want to link to another route programmatically (what I did before usi

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?

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?

Share Improve this question edited May 23, 2017 at 12:02 CommunityBot 11 silver badge asked May 18, 2016 at 17:12 nburknburk 22.7k19 gold badges92 silver badges134 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 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.

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/

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
};
import { withRouter } from 'react-router-dom'

react-router v4.x

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1738707719a4070605.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信