Loading...
Please waitLoading...
Please waitpnpm dlx uselab@latest add use-history-state
import { useHistoryState } from "@/hooks/use-history-state"
export function Component() {
const { state, set, undo, redo, clear, canUndo, canRedo } = useHistoryState({
count: 0,
})
return (
<div>
<p>Count: {state.count}</p>
<button onClick={() => set({ count: state.count + 1 })}>Increment</button>
<button onClick={undo} disabled={!canUndo}>
Undo
</button>
<button onClick={redo} disabled={!canRedo}>
Redo
</button>
<button onClick={clear}>Clear</button>
</div>
)
}initialPresent:
present value.{}) if not provided.state:
present value).set:
present to the past and setting newPresent as the new present.undo:
past state to present and the current present to future.redo:
future state to present and the current present to past.clear:
present value and clears past and future.canUndo:
past.length > 0).canRedo:
future.length > 0).useHistoryStateexport function useHistoryState<T extends object = object>(
initialPresent: T = {} as T
): {
state: T
set: (newPresent: T) => void
undo: () => void
redo: () => void
clear: () => void
canUndo: boolean
canRedo: boolean
}The internal state of the hook is structured as follows:
{
past: T[]; // Array of past states
present: T; // Current state
future: T[]; // Array of future states (for redo)
}Initial State:
past: []present: { count: 0 }future: []After set({ count: 1 }):
past: [{ count: 0 }]present: { count: 1 }future: []After undo:
past: []present: { count: 0 }future: [{ count: 1 }]After redo:
past: [{ count: 0 }]present: { count: 1 }future: []After clear:
past: []present: { count: 0 }future: []| Name | Type | Description | Default Value | Optional |
|---|---|---|---|---|
initialPresent | T | The initial state for the present value. Defaults to an empty object ({}). | {} | Yes |
| Name | Type | Description |
|---|---|---|
state | T | The current state (i.e., the present value). |
set | (newPresent: T) => void | A function to update the state. Adds the current present to the past and sets newPresent as the new present. |
undo | () => void | A function to undo the last state change. Moves the last past state to present and the current present to future. |
redo | () => void | A function to redo the last undone state change. Moves the first future state to present and the current present to past. |
clear | () => void | A function to reset the state to the initial present value and clear past and future. |
canUndo | boolean | Indicates whether an undo operation is possible (past.length > 0). |
canRedo | boolean | Indicates whether a redo operation is possible (future.length > 0). |