文档

订阅 React 状态

在 React 控件中订阅编辑器状态。

试一试

“测试” 长度为 2。再输入一个字符检查计数更新。

交互示例
示例结果

添加到编辑器

用 useEditorState 的 selector 读取由编辑器派生的值。

import { EditorContent, EditorProvider, useEditor, useEditorState } from 'yurich';

export function CountedEditor() {
  const editor = useEditor({
    extensions: [],
    initialValue: "<p>测试</p>",
  });
  const length = useEditorState(editor, instance => instance.el?.textContent?.length ?? 0);

  return (
    <EditorProvider editor={editor}>
      <EditorContent ariaLabel="编辑器" />
      <p>字符数 {length}</p>
    </EditorProvider>
  );
}

行为说明

简单指示器返回数字或字符串。用 useIsActive 或历史钩子读取专用状态。

功能