是否可以组合嵌套有以下结构的减速器:
import 'user' from ...
import 'organisation' from ...
import 'auth' from ...
// ...
export default combineReducers({
auth: {
combineReducers({
user,
organisation,
}),
auth,
},
posts,
pages,
widgets,
// .. more state here
});
国家的结构如下:
{
auth: {
user: {
firstName: 'Foo',
lastName: 'bar',
}
organisation: {
name: 'Foo Bar Co.'
phone: '1800-123-123',
},
token: 123123123,
cypher: '256',
someKey: 123,
}
}
当auth
减速机具有结构:
{
token: 123123123,
cypher: '256',
someKey: 123,
}
所以也许散布算子很方便吗?...auth
不确定 :-(
完美地结合使用嵌套的化径器combineReducers
。但是还有另一种非常有用的模式:嵌套化归约器。
const initialState = {
user: null,
organisation: null,
token: null,
cypher: null,
someKey: null,
}
function authReducer(state = initialState, action) {
switch (action.type) {
case SET_ORGANISATION:
return {...state, organisation: organisationReducer(state.organisation, action)}
case SET_USER:
return {...state, user: userReducer(state.user, action)}
case SET_TOKEN:
return {...state, token: action.token}
default:
return state
}
}
在上面的示例中,authReducer
可以将操作转发到状态organisationReducer
并userReducer
更新其状态的某些部分。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句