Skip to main content

LexicalComposer

The LexicalComposer component is the root context provider for Lexical React applications. It creates and initializes a Lexical editor instance and provides it to child components via React context.
import { LexicalComposer } from '@lexical/react/LexicalComposer';

function App() {
  const initialConfig = {
    namespace: 'MyEditor',
    theme: myTheme,
    onError: (error) => console.error(error),
  };

  return (
    <LexicalComposer initialConfig={initialConfig}>
      {/* Your editor components and plugins */}
    </LexicalComposer>
  );
}

Props

initialConfig
InitialConfigType
required
Configuration object for the editor
children
React.ReactNode
required
Child components that will have access to the editor context.

LexicalComposerContext

Context object that provides the editor instance and theme accessor to child components.

useLexicalComposerContext

Hook to access the editor instance and context from within a LexicalComposer.
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';

function MyComponent() {
  const [editor, context] = useLexicalComposerContext();

  // editor is the LexicalEditor instance
  // context provides theme access via context.getTheme()

  return <div>...</div>;
}
Returns: [LexicalEditor, LexicalComposerContextType]
  • LexicalEditor - The editor instance
  • LexicalComposerContextType - Context object with getTheme() method
Throws: Error if called outside of a LexicalComposer

LexicalNestedComposer

Context provider for nested editors (editors within decorators or other editors).
import { LexicalNestedComposer } from '@lexical/react/LexicalNestedComposer';
import { createEditor } from 'lexical';

function NestedEditor() {
  const nestedEditor = createEditor({
    namespace: 'nested',
    parentEditor: $getEditor(),
  });

  return (
    <LexicalNestedComposer initialEditor={nestedEditor}>
      {/* Nested editor plugins and components */}
    </LexicalNestedComposer>
  );
}

Props

initialEditor
LexicalEditor
required
The nested editor instance, created with createEditor().
children
React.ReactNode
Child components and plugins for the nested editor. Note: plugins are not inherited from the parent editor.
initialTheme
EditorThemeClasses
Optional theme to override the editor’s theme.
initialNodes
ReadonlyArray<Klass<LexicalNode> | LexicalNodeReplacement>
deprecated
Deprecated: This feature is not safe and will be removed in v0.32.0. Configure nodes when creating the editor instead.
skipCollabChecks
true | undefined
If true, renders children immediately even if collaboration plugin is active. Otherwise waits for collab to be ready.
skipEditableListener
true | undefined
If true, the nested editor manages its own editable state. Otherwise, it inherits from the parent editor.

createLexicalComposerContext

Utility function to create a composer context object. Used internally by LexicalComposer and LexicalNestedComposer.
function createLexicalComposerContext(
  parent: LexicalComposerContextWithEditor | null | undefined,
  theme: EditorThemeClasses | null | undefined,
): LexicalComposerContextType

Parameters

parent
LexicalComposerContextWithEditor | null
Parent context for nested composers, or null for root composers.
theme
EditorThemeClasses | null
Theme configuration. If null, inherits from parent.

Returns

LexicalComposerContextType object with a getTheme() method that returns the current theme.

Build docs developers (and LLMs) love