Reactive React state
Subscribe to editor state from React controls.
Try it
The example starts with “Test” and displays a length of 4. Type another character and watch the counter update. The selector reads el only when it is available.
Live example
Example result
Add it to your editor
Use useEditorState when your UI depends on a derived value from the editor. Keep reading editor values inside the selector so the component is subscribed to updates.
import { EditorContent, EditorProvider, useEditor, useEditorState } from 'yurich';
export function CountedEditor() {
const editor = useEditor({
extensions: [],
initialValue: "<p>Test</p>",
});
const length = useEditorState(editor, instance => instance.el?.textContent?.length ?? 0);
return (
<EditorProvider editor={editor}>
<EditorContent ariaLabel="Editor" />
<p>Text length {length}</p>
</EditorProvider>
);
}
Behavior to account for
Return a primitive for a small status value. For formatting buttons and history controls, prefer useIsActive, useCanUndo, and useCanRedo. Use useEditorContext when a control receives the editor through EditorProvider.