useHistoryState
Manages state with undo/redo history tracking and configurable history capacity.
Loading preview...
The useHistoryState hook manages a state value alongside bounded past and future stacks, providing undo, redo, and clear helpers for reversible user actions.
Installation
Usage
Examples
Text Editor with Undo and Redo
Use canUndo and canRedo to enable or disable toolbar buttons as the user edits state.
Limiting History Capacity
Pass a capacity argument to cap the maximum number of undo snapshots retained in memory.
API Reference
UseHistoryStateReturn
Current state, history stacks, and undo/redo controls returned by the hook.
Property
Specification
stateT
Current present state value
pastarray
Array of previous state snapshots
Full Signature
futurearray
Array of undone state snapshots available for redo
Full Signature
canUndounion
Whether there is at least one past state to undo to
Full Signature
canRedounion
Whether there is at least one future state to redo to
Full Signature
setfunction
Updates the present state and pushes the previous state onto
pastFull Signature
Parameters
nextStateT | ((prevPresent: T) => T)undofunction
Steps backward to the most recent state in
pastFull Signature
redofunction
Steps forward to the next state in
futureFull Signature
clearfunction
Clears
past and future history while keeping the current stateFull Signature
resetfunction
Resets
state to the initial value (or a new provided value) and clears historyFull Signature
Parameters
newInitialValue?T | undefined