# 撤销与重做 {#history}

Source: [为 React 编辑器添加撤销和重做 · Yurich](https://leeyujoo.com/yurich/zh-cn/features/history/)

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

Package version: `0.1.2`

将撤销与重做按钮接入编辑历史。

## 试一试 {#try-it}

编辑后点击 **撤销**，再点击 **重做**。内容和选区一并恢复。

## 添加到编辑器 {#setup}

历史记录内置。用 `useCanUndo` 和 `useCanRedo` 更新按钮状态。

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

export function HistoryEditor() {
  const editor = useEditor({
    extensions: [],
    initialValue: "<p>删掉这一行试试。还能撤销。</p>",
    historyLimit: 50,
  });
  const canUndo = useCanUndo(editor);
  const canRedo = useCanRedo(editor);

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="编辑历史">
        <button type="button" disabled={!canUndo} onClick={() => editor.history.undo()}>
          撤销
        </button>
        <button type="button" disabled={!canRedo} onClick={() => editor.history.redo()}>
          重做
        </button>
      </div>
      <EditorContent ariaLabel="编辑器" />
    </EditorProvider>
  );
}
```

## 行为说明 {#behavior}

`historyLimit` 默认 100。用 `setHTML(html, { history: false })` 加载新文档可重置历史。

[功能](https://leeyujoo.com/yurich/zh-cn/features/)
