在使用组合时正确链接减速器可能不合适
Properly chaining reducers when using combining them may not be appropriate
在我的应用程序中,状态树的形状是这样的:
{
...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: ' '
})
})
})
})
}
为了更新用户的状态,我一直在编写我的减速器如下:
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
reducer 与 room
结合起来,尽管它具有 id
和 name
属性?如果没有,是否有更好的方法来管理我的状态,使减少更易于管理?
这是您如何构建状态的问题。如果像这样规范化,编辑用户会容易得多:
{
...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'
})
})
}
在我的应用程序中,状态树的形状是这样的:
{
...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: ' '
})
})
})
})
}
为了更新用户的状态,我一直在编写我的减速器如下:
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
reducer 与 room
结合起来,尽管它具有 id
和 name
属性?如果没有,是否有更好的方法来管理我的状态,使减少更易于管理?
这是您如何构建状态的问题。如果像这样规范化,编辑用户会容易得多:
{
...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'
})
})
}