use-undo
React Hooks to implement Undo and Redo functionality.
Requirement ⚠️
To use useUndo
, you have to use at minimum react@16.7.0-alpha.0
. React Hooks is currently at RFC stage.
Installation
Usage
API
useUndo
state
Type: Object
Key | Type | Description |
---|---|---|
past | Array |
The undo stack. |
present | Any |
The present state. |
future | Array |
The redo stack. |
actions
Type: Object
Key | Type | Description |
---|---|---|
set | function |
Assign a new value to present . |
reset | function |
Clear past array and future array. Assign a new value to present . |
undo | function |
See handling-undo. |
redo | function |
See handling-redo. |
canUndo | boolean |
Check whether state.undo.length is 0 . |
canRedo | boolean |
Check whether state.redo.length is 0 . |
How does it work?
Refer to Redux Implementing Undo History, use-undo
implements the same concect with useReducer
.
The state structure looks like:
It stores all states we need. To operate on this state, there are three functions in actions
(set
, undo
and redo
) that dispatch defined types and necessary value.