免责声明:此问题针对特定的软件包reduxsauce
采取经典的redux动作,通过分派单个动作,它将流经所有化简器,如果我们要更新状态,我们会在合适的情况下捕获每个化简器中的类型
loginPage.js
this.props.memberLogin({ name: 'john' }); //{ type: MEMBER_LOGIN, payload: { name: 'john' } }
LoginAction.js
const memberLogin = member => {
return { type: MEMBER_LOGIN, payload: member }
}
authReducer.js
const INITIAL_STATE = { isLoggedIn: false }
switch(state = INITIAL_STATE, action) {
case MEMBER_LOGIN: return { ...state, isLoggedIn: true };
default: return state;
}
memberReducer.js
const INITIAL_STATE = { member: null }
switch(state = INITIAL_STATE, action) {
case MEMBER_LOGIN: return { ...state, member: action.payload };
default: return state;
}
想知道通过使用reduxsauce
,我们还能实现与以上所示类似的东西吗? (发送单个动作并更新两个减速器)
答案 0 :(得分:1)
是的,可以。
我创建了this Snack example来为您提供帮助,但要点是您必须配置减速器以监听相同的动作。
Kinda像这样:
const reduceA = (state, action) => ({
...state,
a: action.value,
});
const reduceB = (state, action) => ({
...state,
b: action.value,
});
const { Types, Creators: actionCreators } = createActions({
testAction: ['value'],
});
const HANDLERS_A = {
[Types.TEST_ACTION]: reduceA,
};
const HANDLERS_B = {
[Types.TEST_ACTION]: reduceB,
};
在该示例中,减速器A和B的状态值都通过相同的动作testAction
进行更改。