Need help with use-undo?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

homerchen19
304 Stars 17 Forks MIT License 35 Commits 8 Opened issues

Description

React Hooks to implement Undo and Redo functionality

Services available

!
?

Need anything else?

Contributors list

# 36,784
JavaScr...
React
messeng...
dnd
27 commits
# 580,525
React
JavaScr...
react-h...
hooks
1 commit
# 430,843
React
JavaScr...
react-h...
smartho...
1 commit

♻️ use-undo

undo/redo functionality with React Hooks.

screensho

Installation

yarn add use-undo

Usage

Edit use-undo-demo

import React from 'react';
import ReactDOM from 'react-dom';
import useUndo from 'use-undo';

const App = () => { const [ countState, { set: setCount, reset: resetCount, undo: undoCount, redo: redoCount, canUndo, canRedo, }, ] = useUndo(0); const { present: presentCount } = countState;

return (

You clicked {presentCount} times

setCount(presentCount + 1)}> + setCount(presentCount - 1)}> - undo redo resetCount(0)}> reset to 0
); };

API

useUndo

const [state, actions] = useUndo(initialState);

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:
{
  past: Array,
  present: ,
  future: Array
}

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.

Related repo

License

MIT © homerchen19

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.