Skip to main content

Function: useUndoableState()

useUndoableState<T>(initial, options?): [T, UndoableSetter<T>, UndoableReset<T>]

Defined in: use-undoable-state.ts:58

History-aware analogue of useState. Each call to the setter pushes a redo/undo pair onto the surrounding Amnesia store so Ctrl+Z restores the previous value.

The hook owns the underlying React state, so undo/redo work entirely through this hook's setter — no external store required. The setter and reset references are stable across renders.

Type Parameters

Type Parameter
T

Parameters

ParameterType
initialT | (() => T)
optionsUseUndoableStateOptions<T>

Returns

[T, UndoableSetter<T>, UndoableReset<T>]

Example

const [title, setTitle, resetTitle] = useUndoableState("Untitled", {
label: "Edit title",
coalesceKey: "edit:title",
});
// ...
resetTitle(); // restore "Untitled" and clear history
resetTitle("New document"); // overwrite, clear history

reset clears the entire scope the hook is bound to — including entries pushed by other hooks or imperative useAmnesia().push(...) calls in the same scope. Pin sensitive history to its own scopeId when that boundary matters.