使用组合时适当链接减速器可能不合适

时间:2016-02-05 01:13:55

标签: flux redux immutable.js

在我的应用程序中,状态树具有以下形状:

{
    ...otherReducers,
    rooms: Map({
        // room being the room's id
        room: Map({
             id: '',
             name: '',
             users: Map({
                 // user being the user's id
                 user: Map({
                     id: '',
                     name: '',
                     rank: ' '
                 })
             })
        })
    })
}

为了更新用户的状态,我一直在编写我的Reducer,如下所示:

function roomsReducer(state = Map(), action) {
    case USER_RENAME:
        return selectRoom(state, action);
    default:
        return state;
}

function selectRoom(state, action) {
    let room = state.get(action.payload.id);
    return state.set(action.payload.id, roomReducer(room, action));
}

// and so on and so forth until I reach the user I want to update

我是否可以将users缩减器与room合并,尽管它具有idname属性?如果没有,是否有更好的方法来管理我的状态以减少更易于管理?

1 个答案:

答案 0 :(得分:1)

这是您构建州的方式的问题。如果将其标准化,则编辑用户会更容易:

{
  ...otherReducers,
  rooms: Map({
    5: Map({
      id: '5',
      name: 'some name',
      users: [1,4,52]
    })
  }),
  users: Map({
    1: Map({
      id: '1',
      name: 'Charles',
      range: 'Master'
    }),
    4: Map({
      id: '4',
      name: 'Sally',
      range: 'Master'
    }),
    52: Map({
      id: '52',
      name: 'Harry',
      range: 'Novice'
    })
  })
}