---
name: Usage
---

import { Playground } from 'docz';
import useSimpleUndo from '../src';

# Usage

```js
import useSimpleUndo from 'use-simple-undo';
```

```jsx
const Example = () => {
  const [state, cursor, setValue, { undo, redo }] = useSimpleUndo(0);

  const value = state[cursor];

  const increment = () => setValue(value + 1);
  const decrement = () => setValue(value - 1);

  return (
    <>
      <div>value: {value}</div>
      <div>
        {state.map((v, i) => (
          <span key={i} style={{ fontWeight: i === cursor ? 900 : 100 }}>
            {v}
          </span>
        ))}
      </div>
      <div>cursor: {cursor}</div>
      <button onClick={increment}>+</button>
      <button onClick={decrement}>-</button>
      <button onClick={undo}>undo</button>
      <button onClick={redo}>redo</button>
    </>
  );
};
```

<Playground>
  {() => {
    const [state, cursor, setValue, { undo, redo }] = useSimpleUndo(0);
    const value = state[cursor];
    const increment = () => setValue(value + 1);
    const decrement = () => setValue(value - 1);
    return (
      <>
        <div>value: {value}</div>
        <div>
          {state.map((v, i) => (
            <span key={i} style={{ fontWeight: i === cursor ? 900 : 100 }}>
              {v}
            </span>
          ))}
        </div>
        <div>cursor: {cursor}</div>
        <button onClick={increment}>+</button>
        <button onClick={decrement}>-</button>
        <button onClick={undo}>undo</button>
        <button onClick={redo}>redo</button>
      </>
    );
  }}
</Playground>
