use-temporal-state
Version:
A lightweight React hook for undo/redo state management with history compression.
21 lines (20 loc) • 693 B
TypeScript
interface TemporalOptions<T> {
limit?: number;
shouldAddToHistory?: (prev: T, next: T) => boolean;
}
/**
* useTemporalState is a custom React hook that manages state with undo/redo capabilities,
* and includes optional history compression to limit memory usage.
*
* @param initialValue The initial state value.
* @param options Optional configuration for limiting history and controlling when changes are recorded.
*/
export default function useTemporalState<T>(initialValue: T, options?: TemporalOptions<T>): {
state: T;
set: (newState: T | ((prev: T) => T)) => void;
undo: () => void;
redo: () => void;
canUndo: boolean;
canRedo: boolean;
};
export {};