I would like my reducers to populate a root object in the state, independently from the slice of state they operate on.
I have found many pages explaining how wonderful is normalizr, but no one is explaining where and how to store this normalized data.
Questions are:
am I trying to do something unusual and wrong?
how can I modify state in the root state object, since reducers only operate on a slice of the data.
so videos reducer:
const videos = (state, action) => {
switch (action.type) {
case 'LOAD_VIDEOS': {
return ....
}
}
}
should populate not only state.videos
(with a array of video id), but also state.database.videos
(and other keys as well if video contains other entities) with normalized data.
If your reducer needs to work on more than a slice of state, give it the whole state.
If you're using combineReducers()
, you can wrap it so you keep the advantages of combining reducers and you can still have a reducer that works on the full state:
function somePartOfState(state, action) {
// ...
}
function otherPartOfState(state, action) {
// ...
}
function fullState(state, action) {
// ...
}
const combined = combineReducers({
somePartOfState,
otherPartOfState
});
export function rootReducer(state, action) {
// call the combined reducers,
// then pass the resulting state to the fullState reducer
return fullState(combined(state, action), action);
}