如何在Redux操作结束时导航?

时间:2016-08-30 23:11:46

标签: redux react-redux

我只想使用ReactReact ReduxReact RouterRedux Thunk

我想在调度成功的用户创建操作时导航到仪表板页面。这是我的异步动作创建者,

export function createUser() {
  return dispatch => {
    dispatch(creatingUser());
    return axios.post('/api/users').then(() => {
      // how to navigate to dashboard after this action is dispatched?
      dispatch(createdUser());
    });
  };
}

你能告诉我我应该在哪里naviage programmatically吗?

1 个答案:

答案 0 :(得分:0)

最初看,我希望“createdUser”返回一个承诺(如@idbehold之前所说)

简而言之,就像这样。

// make sure your function createdUser returns a promise
function createdUser() {
 return new Promise((resolve, reject) => {
   //simulate some api request
   setTimeout( () =>{
     // api resolves. in the .then() do:
     resolve()
   }, 4000)
 })
}

// the dispatch will forward resolution of the promise ahead into the 
// .then.. then you can redirect.
dispatch(createdUser()).then( ()=> {
  console.log("NAVIGATING AWAY")
  //browserHistory.push('/some/path')
  //assuming you are importing browserHistory
})

我希望我能提供帮助,如果没有:-(,也许我并不完全明白你的需求是什么。让我知道,我会尽力帮助你。

相关问题