August 2017
Beginner
374 pages
10h 41m
English
We want to be able to undo/redo actions, so we will need to store a history of states (instead of just storing the current state). Consider the following:
state = 0
Instead of the preceding code, our initial state might look something like this:
state = [0]
After a few actions, the state might look like this:
state = [0, 1, 2, 3, 2]
This will show 2 on the counter (as it is the latest state).
However, in addition to the history, we will also need to store a pointer to the current state. Otherwise, after some undo/redo actions, we have no way to tell which state in the history is the current state.
Let's turn our state into an object, storing the history array and the currentState index:
state = { history: [0], currentState: ...Read now
Unlock full access