Recursively Prune State on store.replaceReducer #2672
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #1636 -- Prevents combineReducers from warning about unexpected keys after calling replaceReducer.
Adds recursive
pruneState
function to createStore, which uses the differences between the current and next state shapes to determine which state branches have been removed (are not acknowledged by the next reducer). The removed state branches are then removed from the current state.The state shape is defined here as the union of every path which begins at the state tree root and terminates at a non-plain-object leaf (boolean, number, string, array, etc).
Properties of a plain object which do not appear in the current or next shape are not pruned. This covers the use case where a plain object is being used as a dynamic collection of key-value pairs.
If any plain object is unchanged by the pruning process, the original object is returned instead of a new one.
The state shape for a given reducer is determined by invoking it with an undefined state and the INIT action.
The combineReducers warning does not appear in production, so this pruning process also does not take place in production.
Added unit test which fails (generates warning) when the pruning process does not take place.