createReactiveCommandStack()
createReactiveCommandStack<
S>(options):object
Creates a reactive command stack with undo/redo support.
Unlike createCommandStack (imperative, mutation-based), this variant
manages state internally and notifies via onChange on every transition.
Commands are pure functions from state to state, making it naturally
compatible with React, Vue, Angular, or any reactive framework.
Type Parametersβ
S: Sβ
Parametersβ
options: ReactiveCommandStackOptions<S>β
Initial state and onChange callback
Returnsβ
Stack with execute, undo, redo, canUndo, canRedo, clear, state
execute(): (cmd) => voidβ
Execute a state command. Clears redo stack.
cmd:
UndoableStateCommand<S>
Returns:void
undo(): () => booleanβ
Undo the last command. Returns false if nothing to undo.
Returns: booleanβ
redo(): () => booleanβ
Redo the last undone command. Returns false if nothing to redo.
Returnsβ
boolean
state: #### Get Signature: > get state(): Sβ
Current state.
Returnsβ
S
canUndoβ
Get Signature: > *get canUndo*(): booleanβ
Returnsβ
boolean
canRedoβ
Get Signature: > *get canRedo*(): booleanβ
Returns: booleanβ
clear(): () => voidβ
Reset to initial state and clear all history.
Returns: voidβ
Sinceβ
2.5.0
Exampleβ
// React
const [board, setBoard] = useState(INITIAL);
const stack = useRef(createReactiveCommandStack({
initial: INITIAL,
onChange: setBoard,
}));
stack.current.execute(undoableState(
(b) => moveTask(b, "todo", "done", task),
(b) => moveTask(b, "done", "todo", task),
));
// setBoard is called automatically with the new state
stack.current.undo(); // setBoard called with previous state
stack.current.redo(); // setBoard called with next state
ReactiveCommandStackOptions<S>β
Options for createReactiveCommandStack.
Sinceβ
2.5.0
Type Parametersβ
S: Sβ
Propertiesβ
initial: Sβ
The initial state.
onChange(): (state) => voidβ
Called after every state change (execute, undo, redo, clear).
state:
S
Returns:void