如何在 Redux 操作结束时导航?
How to navigate at the end of a Redux action?
我只想使用 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的确切位置吗?
最初看起来,我希望 "createdUser" returns 一个承诺(就像@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
})
我希望我对您有所帮助,如果没有 :-( ,也许我没有完全理解您的需求 is/was。让我知道,我会尽力提供进一步的帮助。
我只想使用 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的确切位置吗?
最初看起来,我希望 "createdUser" returns 一个承诺(就像@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
})
我希望我对您有所帮助,如果没有 :-( ,也许我没有完全理解您的需求 is/was。让我知道,我会尽力提供进一步的帮助。