我不知道。可以将路由器重定向到Redux Reducer中的某些组件吗?
例如:
case SUCCESS:
return { ...state, loading: false, push('/some')};
我正在使用react-router,react-router-redux。
答案 0 :(得分:2)
您仅应使用reducer开关来修改redux状态。诸如重定向之类的任何其他操作都只能在分派之前在操作中完成。
如果您正在使用redux-thunk,则可以执行以下操作:
export function myAction(data, history) {
return (dispatch) => {
dispatch({
type: SUCCESS,
data,
}).then((response) => {
history.push("/some")
});
};
}
请确保在调用操作时传递this.props.history
。
答案 1 :(得分:1)
Reducer必须是纯函数,这意味着您不能直接从状态重定向。
要实现此功能,请尝试使用redux-saga或redux-thunk
答案 2 :(得分:0)
我认为减速器不可能做到这一点,但是您可以做的另一件事是。在状态中有一个属性,您想将其重定向到要处理的路由,以便对于需要您重定向用户的任何操作,将该属性设置为您选择的路由。但我看不出这样做的原因,而您想解决什么问题