我只想使用React
,React Redux
,React Router
和Redux 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吗?
答案 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
})
我希望我能提供帮助,如果没有:-(,也许我并不完全明白你的需求是什么。让我知道,我会尽力帮助你。