嵌套Redux减速器

安德鲁·麦克拉根(AndrewMcLagan):

是否可以组合嵌套有以下结构的减速器:

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可以将操作转发到状态organisationReduceruserReducer更新其状态的某些部分。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章