Skip to main content

useHistory

useHistory is a custom hook to manage state with history tracking, supporting undo/redo functionality.

Usage

import { useHistory } from "hookify-react";

export default function UseHistoryExample() {
const [value, setValue, { history, pointer, back, forward, go }] = useHistory(0, { capacity: 5 });

return (
<div style={{ textAlign: "center", fontFamily: "Arial, sans-serif" }}>
<h2>useHistory Hook Example</h2>
<p>Current Value: <strong>{value}</strong></p>
<button onClick={() => setValue(prev => prev + 1)}>Increment</button>
<button onClick={() => setValue(prev => prev - 1)}>Decrement</button>
<button onClick={() => setValue(0)}>Reset</button>
<hr />
<button onClick={back} disabled={pointer === 0}>Undo</button>
<button onClick={forward} disabled={pointer === history.length - 1}>Redo</button>
<button onClick={() => go(0)} disabled={pointer === 0}>Go to Start</button>
<p>History: {JSON.stringify(history)}</p>
<p>Pointer: {pointer}</p>
</div>
);
}

API Reference

Parameters

ParameterTypeDefaultDescription
defaultValue`T(() => T)`
optionsTUseHistoryOptions{ capacity: 10 }Configuration options for history tracking.
options.capacitynumber10The maximum number of history states to keep.

Return Value

useHistory returns a tuple with the following values:

IndexPropertyTypeDescription
0stateTThe current state value.
1set`(value: T((prev: T) => T)) => void`
2historyObj{ history: T[], pointer: number, back: () => void, forward: () => void, go: (index: number) => void }An object containing history management utilities.