如何在ReactRouter 4中的组件外导航?

时间:2017-06-13 18:22:51

标签: reactjs react-router

在React Router(v3)中,我可以接受服务器响应,并使用browserHistory.push转到相应的响应页面。但是,这在v4中是不可用的,我不确定在React-Router V4中处理此问题的适当方法是什么。

在下面的示例中,当服务器返回成功时,用户将进入购物车页面

// actions / index.js

export function addProduct(props) {
  return dispatch =>
    axios.post(`${ROOT_URL}/cart`, props, config)
      .then(response => {
        dispatch({ type: types.AUTH_USER });
        localStorage.setItem('token', response.data.token);
        browserHistory.push('/cart'); // no longer in React Router V4
      });
}

如何从React Router v4中的功能重定向到页面?

我可以在我的案例中找到很多关于导航的参考资料,我需要从函数中导航

1 个答案:

答案 0 :(得分:-2)

如果您希望程序路由器到达某个地方,使用历史记录是最佳解决方案,这里的问题是如何在组件中获取历史记录对象:

  • 路线:使用路线
  • 渲染组件
  • WithRouter :wrap component:

    class yourcomponent extends Component {}

    withRouter()(yourcomponent)

如果您想在 Redux 中执行此操作,则可以尝试 React-Router-Redux

相关问题