A React hook providing Undo and Redo functionalities.
Live example on codesanbox
$ npm install usedo
or
$ yarn add usedo
This hook works just like the useState hook with the addition of four extra parameters.
const [state, setState, undo, redo, canUndo, canRedo] = useDo(initialState)
undo
(function) : reverts the state to its previous valueredo
(function) : reverts the undocanUndo
(boolean) : current change can be undonecanRedo
(boolean) : current undo can be redone
The canUndo and canRedo can be useful to determine if an 'Undo' or a 'Redo' action should be disabled.
import React from 'react'
import useDo from 'usedo'
function App() {
const [name, setName, undo, redo, canUndo, canRedo] = useDo('')
return (
<form>
<input
type="text"
name="name"
value={name}
onChange={evt => setName(evt.target.value)}
/>
<input type="button" value="Undo" onClick={undo} disabled={!canUndo} />
<input type="button" value="Redo" onClick={redo} disabled={!canRedo} />
</form>
)
}