最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Router v4 navigation programmatically - Stack Overflow

programmeradmin1浏览0评论

When I use react router with Redirect, Link or NavLink eveything is fine. But I need a function that navigates me to a route programmatically.

That is what I want :

if(a == 1){
    this.context.history.push("/")
}
else {
    this.context.history.push("/home");
}

As you see context history is not exists in react router v4 any more. I cannot write withroute in js code for navigation. Can anyone say me how to navigate directly in js code?

Thanks

When I use react router with Redirect, Link or NavLink eveything is fine. But I need a function that navigates me to a route programmatically.

That is what I want :

if(a == 1){
    this.context.history.push("/")
}
else {
    this.context.history.push("/home");
}

As you see context history is not exists in react router v4 any more. I cannot write withroute in js code for navigation. Can anyone say me how to navigate directly in js code?

Thanks

Share Improve this question edited Nov 16, 2020 at 13:34 Can PERK asked Apr 29, 2017 at 12:25 Can PERKCan PERK 63010 silver badges26 bronze badges 3
  • Did you try using React Router's browserHistory ? – Lingaraju E V Commented Apr 29, 2017 at 13:05
  • I am using react-router-dom i mean v4. I cannot ser any solution with it – Can PERK Commented Apr 29, 2017 at 22:29
  • 1 Does this answer your question? Programmatically navigate using React router – majorobot Commented Apr 30, 2021 at 15:24
Add a ment  | 

1 Answer 1

Reset to default 9

The router will add a history to your props, you can use it like this:

this.props.history.push('/mypath')

Check this for more info: Navigating Programatically in React-Router v4

发布评论

评论列表(0)

  1. 暂无评论