- 每一个
reducer
会根据combineReducers
中设置的key
在处理中获得一个独立subState
- 每一个
reducer
处理subState
的时候不要返回nil - 所有未匹配到的
action
,必须把它接收到state
原封不动返回
Reducer SubReducer01 = ^SubState01 * (id<Action> action, SubState01 *state) {
if (state == nil) {
// 需要处理nil 返回初始值
SubState01 *nextState = [SubState01 new];
return nextState;
}
if ([state isKindOfClass:[SubState01 class]]) {
// do something
}
return state;
};
Reducer SubReducer02 = ^ SubState02 * (id<Action> action, SubState02 *state) {
if (state == nil) {
// 需要处理nil 返回初始值
SubState02 *nextState = [SubState02 new];
return nextState;
}
if ([state isKindOfClass:[SubState02 class]]) {
// do something
}
return state;
};
#import <Redux/Combine.h>
Reducer reducer = combineReducers(@{kSubState01: SubReducer01,
kSubState02: SubReducer02,
...
});
Store *store = [[Store alloc] initWithReducer:reducer
state:nil
middlewares:@[middleware01, middleware02, ...]];
- (void)updateState:(CombinedState *)state {
// 依次处理每个sub state
SubState01 subState01 = [state subStateForKey:kSubState01];
...
}
或者如果不想回调CombinedState,可以再subscribe的时候传入transForm,可以参见ComplexCounter Demo