将React Router的道具与MobX商店同步

时间:2019-04-11 11:22:30

标签: reactjs mobx

注册失败后,我想重定向。
我正在使用Mobx。
所以有一个问题。
当我搜索重定向时,人们说使用下面的代码。

this.props.history.push("/");

当它在自己的组件中时可以使用。
但就我而言,我正在mobx store的内部运行它。
然后,this.props不是route的props组件。
所以我想知道如何通过mobx商店读取路线的历史记录。
非常感谢!

// In the mobx store

 @action submitSignIn = () => {
    axios
      .post('auth/join', { email: this.initialState.register.email })
      .then((response) => {
        if (response.data === true) {
          this.props.history.push('/signin');
        }
      .catch((error) => {
        console.log(error);
      });
  };

1 个答案:

答案 0 :(得分:1)

一种解决方法是使用history库中的createBrowserHistory函数手动创建history对象,并将该对象提供给{ Router组件。

然后,您可以在应用程序中的任何位置使用此history对象以编程方式控制路由。

示例

history