useHistoryState

Add undo / redo functionality with useHistoryState.

Install:

npm i @uidotdev/usehooks

Description:

The useHistoryState hook is useful for managing state with undo and redo capabilities in React components. The hook offers functions like undo, redo, set, and clear to interact with the state as well as other state related values like canUndo and canRedo.

Parameters

NameTypeDescription
initialPresentobject(Optional) The initial state value. Default: {}.

Return Value

The useHistoryState hook returns an object with the following properties and functions:

NameTypeDescription
stateanyThe current state value.
setfunctionA function to set the state value.
undofunctionA function to undo the previous state.
redofunctionA function to redo the next state.
clearfunctionA function to clear the state history and reset the state.
canUndobooleanIndicates whether an undo action is available.
canRedobooleanIndicates whether a redo action is available.

Demo:

Example:

More Hooks:

Sort: