Skip to content

useStateWithHistory

Manage a state value with it's entire history of updates.

The useStateWithHistory hook is helpful when you want to manage a state value along with it’s entire history of updates.

The hook returns the latest value of state and provide handlers to perform redo, undo, set and reset operations.

useStateWithHistory

Count Value: 0

Usage

Import the hook from @abhushanaj/react-hooks and use in required component.

./src/App.tsx
import { useStateWithHistory } from '@abhushanaj/react-hooks';
function App() {
const [count, { canRedo, canUndo, redo, undo, set, reset }] = useStateWithHistory(0);
const addByOne = () => {
set(count + 1);
};
const subtractByTwo = () => {
set(count - 2);
};
const multiplyByThree = () => {
set(count * 3);
};
const divideByTwo = () => {
set(count / 3);
};
return (
<div>
<div>
<p>Count Value: {count}</p>
</div>
<div>
<button onClick={addByOne}>+1</button>
<button onClick={subtractByTwo}>-2</button>
<button onClick={multiplyByThree}>*3</button>
<button onClick={divideByTwo}>/2</button>
</div>
<div>
<button onClick={undo} disabled={!canUndo}>
Undo
</button>
<button onClick={redo} disabled={!canRedo}>
Redo
</button>
<button onClick={reset}>Reset</button>
</div>
</div>
);
}
export default App;

API Reference

Parameters

ParameterTypeDescriptionDefault
initialValueTThe initial value of the state.N/A

Return Value

The hook returns a tuple with [value, valueUpdateOptions].

ParameterTypeDescriptionDefault
valueTThe current value of state.N/A
valueUpdateOptionsValueUpdateOptionsThe controls for the state with history.N/A

Types Reference

1. ValueUpdateOptions

The control options for useStateWithHistory.

ParameterTypeDescriptionDefault
set(newValue: T)=>voidFunction to set the new value for state.N/A
redo()=>voidFunction to redo the next state.N/A
undo()=>voidFunction to undo the previous state.N/A
reset()=>voidFunction to reset state history and go back to initial state.N/A
canUndobooleanBoolean indicating whether undo action is available.N/A
canRedobooleanBoolean indicating whether redo action is available.N/A