Skip to main content

useUndo

Description#

Undo or Redo functionality.

API (click to expand)

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

state (Object)#

KeyTypeDescription
pastArrayThe undo array
presentAnyThe present state
futureArrayThe redo array

actions (Object)#

KeyTypeDescription
setfunctionAssign a new value to present
resetfunctionClear past array and future array. Assign a new value to present
undofunctionAssign value that was assigned on step earlier
redofunctionAssign value that was assigned one step in the future
canUndobooleanCheck if there are values in the past
canRedobooleanCheck if there are values in the future

Usage#

import { useUndo } from '@springtree/eva-suite-react-hooks';
const Example = () => {  const [    countState,    {      set: setCount,      reset: resetCount,      undo: undoCount,      redo: redoCount,      canUndo,      canRedo,    },  ] = useUndo(0);  const { present: presentCount } = countState;
  return (    <div>      <p>You clicked {presentCount} times</p>      <button key="increment" onClick={() => setCount(presentCount + 1)}>        +      </button>      <button key="decrement" onClick={() => setCount(presentCount - 1)}>        -      </button>      <button key="undo" onClick={undoCount} disabled={!canUndo}>        undo      </button>      <button key="redo" onClick={redoCount} disabled={!canRedo}>        redo      </button>      <button key="reset" onClick={() => resetCount(0)}>        reset to 0      </button>    </div>  );};
export default Example;

References#