I'm expanding the functionality of a local-only React app to hook into an API. Here is my code before the refactor. You'll see I'm setting initial state from a global window variable, INIT_STATE, which also includes some other root-level objects.
I'm focusing in on one of these root-level objects, called "projects".
For the projects reducer, I was normalizing (using normalizr) the state like such and combining reducers to get use reducers for the normalized entities:
window.PROJECTS_STATE = fromJS(normalize(window.INIT_STATE.projects, projectsSchema));
const result = (state = window.PROJECT_STATE.get('result'), action) => {
switch (action.type) {
default:
return state;
}
};
// In these reducers, currently, I'm getting init_state from window.PROJECTS_STATE
// i.e. window.PROJECT_STATE.entities.projects), which has been normalized above
const entities = combineReducers({
projects,
items,
events,
});
// Here is where I'd like to handle an action to normalize and
// overwrite the state of `projects` and all it's sub-reducers
export default () => combineReducers({
entities,
result,
});
In my refactor I've dispatched actions to fetch and receive API requests in my top level container. I can easily set my initial state of the other root-level objects from what used to be in INIT_STATE by handling the action GET_REQUEST_SUCCESS
in those reducers to return that part of the response, but since projects
is normalized and has nested sub-reducers, I'm unsure how to tackle setting this state with combineReducers.
So ultimately, I want to be able to combine reducers while also handling an action that can overwrite the state of all its sub-reducers. Is this possible or is there a better way to approach about this?
Many thanks.