# Reactive React state {#react-state}

Source: [Subscribe to editor state in React · Yurich](https://leeyujoo.com/yurich/features/react-state/)

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

Package version: `0.1.2`

Subscribe to editor state from React controls.

## Try it {#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.

## Add it to your editor {#setup}

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.

```tsx
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 {#behavior}

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`.

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