I have a React/Redux project where I'm using a Web Worker to calculate the next state when an action is dispatched, and then pass the new state directly to the store, like this:
let nextReducer = reducer => {
return (state, action) => {
if (action.type === 'WEBWORKER') {
return action.payload;
} else {
return reducer(state, action);
}
};
};
let store = createStore(nextReducer(rootReducer));
The problem is that when I manually pass the new state to the redux store, the state now has a new reference, and reselect
library cannot memoize correctly the state. This has as a result that the view is always rerendering at every state change.
The thing is that I always thought that the state always had a different reference (even without manually setting it), because the rule is that we should never mutate the original state object, but we have to always return a new one. So, why if I use my rootReducer
as is, reselect
works fine, but when setting it manually it doesn't?