Skip to content

Having trouble with autofocus feature - TypeError: Cannot read properties of undefined (reading 'focus') #891

@softmarshmallow

Description

@softmarshmallow

The editor is typed NonNullable by useCreateBlockNote, but It cannot be accessed (or at least not mounted) on initial hook call.

Screenshot 2024-06-30 at 8 19 46 PM
  const editor = useCreateBlockNote({
    schema: schema,
    initialContent: safevalue(defaultValue),
  });

  useEffect(() => {
    if (!editor) return;
    if (!editor.focus) return;
    editor.focus();
  }, [editor]);

Am I missing something here?

Below will work when called via click event

     <div
          className="prose dark:prose-invert mx-auto w-full"
          onClick={() => editor.focus()}
        >
          <ThemedRichTextEditorContent
            onKeyDown={(e) => {
              // this is required for preventing exit on enter pressed
              e.stopPropagation();
            }}
            editor={editor}
          />
        </div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingprio:midMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions