以编程方式反应路由器v4导航

时间:2017-04-29 12:25:49

标签: javascript reactjs react-router jsx react-router-dom

当我使用带有Redirect的反应路由器时,Link或NavLink eveything就可以了。但我需要一个以编程方式导航我到路线的功能。

这就是我想要的:

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

如您所见,反应路由器v4中不再存在上下文历史记录。我无法在js代码中编写withroute用于导航。谁能说我如何直接在js代码中导航?

由于

1 个答案:

答案 0 :(得分:8)

路由器会为你的道具​​添加history,你可以像这样使用它:

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

点击此处了解详情:Navigating Programatically in React-Router v4