Defining the initial state
Now, we need to think about the state object of the undoable higher-order reducer. You may think that it would be easier to simply store the history in local variables in the function. If we did that, we could retain the state structure in our enhanced reducer, and we would not need to adjust our application to access the state using state.history[state.currentState].
However, as we discussed earlier, we should not cause side effects that affect the calculated state of the reducer. This also applies to higher-order reducers. This is why we need to store all undo/redo data in the Redux state, not just in a local variable. If we did store the state in a local variable, it would cause unpredictable behavior when combining ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access