useUndo
Undo & redo state changes
Usage
const Component = () => {
const [{ past, present, future }, { canRedo, canUndo, redo, reset, set, undo }] = useUndo<number>(0);
return (
<>
<div
style={{
padding: '1rem',
marginBottom: '2rem',
height: 250,
border: '1px solid black',
overflow: 'auto',
}}
>
<pre>{JSON.stringify({ past, present, future }, null, 2)}</pre>
</div>
<div>
<p>Count: {present}</p>
<button onClick={() => set(present - 1)} type='button'>
-1
</button>
<button onClick={() => set(present + 1)} type='button'>
+1
</button>
</div>
<div>
<button disabled={!canUndo} onClick={undo} type='button'>
undo
</button>
<button disabled={!canRedo} onClick={redo} type='button'>
redo
</button>
<button onClick={() => reset(past[0] || 0)} type='button'>
reset
</button>
</div>
</>
);
};
Arguments
Name | Type | Required? |
---|---|---|
value | any | ✅ |
Returns
Index | Name | Description | Type |
---|---|---|---|
0 | future | future state(s) after undo | any[] |
0 | past | previous state(s) | any[] |
0 | present | current state | any |
1 | canRedo | boolean to help understand if redo is available | boolean |
1 | canUndo | boolean to help understand if undo is available | boolean |
1 | redo | sets state to first future state if available | () => void |
1 | reset | resets state to initials state | () => void |
1 | set | sets new state and adds to past state | () => void |
1 | undo | sets state to first past state if available | () => void |