useStateHistory
A custom React hook to manage state history with undo/redo functionality.
You can use the useStateHistory
hook to manage state with built-in undo and redo functionality. This is particularly useful for applications where you need to track changes and allow users to revert to previous states, such as in form inputs or complex data structures.
Example
Loading...
Install
npx shadcn@latest add https://unlogg.com/r/use-state-history.json
pnpm dlx shadcn@latest add https://unlogg.com/r/use-state-history.json
bunx shadcn@latest add https://unlogg.com/r/use-state-history.json
Notes
- Provides a simple way to manage state with built-in undo/redo functionality.
- Includes functions to set the state, go back, go forward, and clear the history.
API Reference
Parameters
Prop | Type | Default |
---|---|---|
options? | { maxHistorySize?: number; } | { maxHistorySize: 50 } |
initialValue? | T | — |
Returns
Prop | Type | Default |
---|---|---|
currentIndex? | number | - |
history? | T[] | - |
canGoForward? | boolean | - |
canGoBack? | boolean | - |
clear? | () => void | - |
go? | (index: number) => void | - |
forward? | () => void | - |
back? | () => void | - |
setValue? | (value: T | (prev: T) => T) => void | - |
value? | T | - |