Skip to main content

Function: useUndoableStateOptional()

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

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

Provider-optional variant of useUndoableState for reusable components that may render with or without an AmnesiaProvider ancestor.

  • With a provider: identical to useUndoableState — every setter call pushes a reversible command onto the pinned scope, and reset clears that scope's history.
  • Without a provider: degrades to plain useState semantics. Nothing throws, no history is recorded, and reset simply overwrites the value.

The equals bail-out applies in both modes, and like useUndoableState the hook pins to options.scopeId (default "default") — it never floats to the active claim.

Type Parameters

Type Parameter
T

Parameters

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

Returns

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

Example

// Inside a component library that cannot assume amnesia is installed:
const [title, setTitle, resetTitle] = useUndoableStateOptional("Untitled", {
label: "Edit title",
coalesceKey: "edit:title",
});