undoableState()
undoableState<
S>(execute,undo): UndoableStateCommand<S>
Creates an undoable state command from an execute/undo pair.
Unlike undoable (imperative thunks), these are pure state transforms.
Type Parameters
S: S
Parameters
execute: StateCommand<S>
Pure function that produces the next state
undo: StateCommand<S>
Pure function that reverses execute
Returns: UndoableStateCommand<S>
An UndoableStateCommand
Since
2.5.0
Example
interface Counter { value: number }
const increment = undoableState`<Counter>`(
(s) => ({ ...s, value: s.value + 1 }),
(s) => ({ ...s, value: s.value - 1 }),
);
UndoableStateCommand<S>
An undoable state command pairs a forward transform with its inverse. Both are pure functions from state to state.
Since
2.5.0
Type Parameters
S: S
Properties
execute: StateCommand<S>
undo
readonlyundo: StateCommand<S>
StateCommand()<S>
StateCommand<
S> = (state) =>S
A state command is a pure function: takes current state, returns next state. No mutation, no side effects. The stack manages the state for you.
Type Parameters
S: S
Parameters
state: S
Returns: S
Since
2.5.0