Making our app history-aware
To properly update the state history when dispatching actions, you need to make your actions "history-aware." This means that instead of returning a new State
object, your actions should return a new StateHistory<State>
object.
The Aurelia Store plugin provides a helper function called nextStateHistory
to simplify this process:
Explanation:
currentState: StateHistory<State>
: The action now receives the entireStateHistory
object as its first argument.newPresentState: State
: We create a newState
object representing the new present state, as we would normally do in a non-history-aware action.return nextStateHistory(currentState, newPresentState);
: We use thenextStateHistory
helper function to create a newStateHistory
object. This function does the following:Moves the current
present
state to thepast
array.Sets the
newPresentState
as the newpresent
state.Clears the
future
array.
Manual Approach (not recommended):
You could manually create the StateHistory
object, but it's more verbose and error-prone:
It's highly recommended to use the nextStateHistory
helper function to ensure that the history is updated correctly.
Last updated
Was this helpful?