# Undo and redo {#history}

Source: [Add undo and redo to a React editor · Yurich](https://leeyujoo.com/yurich/features/history/)

Package: [`yurich`](https://www.npmjs.com/package/yurich)

Package version: `0.1.2`

Connect Undo and Redo controls to the editor history.

## Try it {#try-it}

Type in the editor and click **Undo**, then **Redo**. Undo also restores the saved selection. Use `Mod+Z` to undo, and `Mod+Shift+Z` or `Mod+Y` to redo.

## Add it to your editor {#setup}

History is available on the editor instance. Use `useCanUndo` and `useCanRedo` to make your buttons update after edits.

```tsx
import { EditorContent, EditorProvider, useCanRedo, useCanUndo, useEditor } from 'yurich';

export function HistoryEditor() {
  const editor = useEditor({
    extensions: [],
    initialValue: "<p>Delete this line. Undo has your back.</p>",
    historyLimit: 50,
  });
  const canUndo = useCanUndo(editor);
  const canRedo = useCanRedo(editor);

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="Edit history">
        <button type="button" disabled={!canUndo} onClick={() => editor.history.undo()}>
          Undo
        </button>
        <button type="button" disabled={!canRedo} onClick={() => editor.history.redo()}>
          Redo
        </button>
      </div>
      <EditorContent ariaLabel="Editor" />
    </EditorProvider>
  );
}
```

## Behavior to account for {#behavior}

`historyLimit` controls the undo depth and defaults to 100. Load an unrelated document with `setHTML(html, { history: false })` when the previous document must not be restored through Undo.

[Features](https://leeyujoo.com/yurich/features/)
