在使用 react-router 和 redux 进行异步调度后,我在哪里更新位置 [英] Where do I update the location after an async dispatch with react-router and redux
问题描述
我现在遇到的问题是我不知道如何在FETCH_USER_FULFILLED"发出后更新我的位置(参见 https://github.com/DDecoene/React-flux-example/blob/4ab1503dc92d28108f50481a293104ba5d15a29e/app/actions/userActions.js#L17)
The problem I have now is that I don't know how to get my location to update after "FETCH_USER_FULFILLED" was dispatched (see https://github.com/DDecoene/React-flux-example/blob/4ab1503dc92d28108f50481a293104ba5d15a29e/app/actions/userActions.js#L17)
我想要做的是向 API 发帖,当它正常时,切换到索引页面"
What I'm trying to do, is to do a post to an API and when it is OK, switch to the index 'page'
我也尝试过 redux-router,但我根本没有让它工作.
I've tried redux-router too but I did not get it to work at all.
推荐答案
我建议使用 react-路由器-redux.它带有 push
和 replace
动作,因此您可以在动作创建器中dispatch
它们.这是处理路由更改的一种更简洁的方法,您将在 redux 开发工具中看到正确的操作.假设您使用 redux-thunk 中间件:
I would suggest using react-router-redux. It comes with push
and replace
actions so you can dispatch
them in your action creator. It's a cleaner way to handle route changes and you'll see proper actions in redux dev tools.
Assuming you use redux-thunk middleware:
export const fetchUser = (username, password) => {
return dispatch => {
axios.post(Endpoints.userLogin, {username, password}).then((response) => {
dispatch({type: "FETCH_USER_FULFILLED", payload: response.data});
dispatch(push('/new_location'));
});
};
};
这篇关于在使用 react-router 和 redux 进行异步调度后,我在哪里更新位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!