Skip to content

Cannot update a component (to2) #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mawnir opened this issue Jan 30, 2024 · 3 comments · Fixed by #1304
Closed

Cannot update a component (to2) #552

mawnir opened this issue Jan 30, 2024 · 3 comments · Fixed by #1304
Labels
bug Something isn't working

Comments

@mawnir
Copy link

mawnir commented Jan 30, 2024

Describe the bug
Warning: Cannot update a component (to2) while rendering a different component (Editor). To locate the bad setState() call inside Editor

To Reproduce
I don't know the name, but adding [initialContent] to useBlockNote hook trigger the error

const editor: BlockNoteEditor = useBlockNote({
        initialContent:
            initialContent
                ? JSON.parse(initialContent) //as PartialBlock[]
                : undefined,
        ...
    }, [initialContent]);

Misc

  • Node version: v20.10.0
  • Package manager: pnpm
  • Browser: Firefox
@mawnir mawnir added the bug Something isn't working label Jan 30, 2024
@matthewlipski
Copy link
Collaborator

If I understand correctly, you put initialContent in the deps array to set the editor content whenever initialContent changes. However, you should instead use an effect hook to reset the editor content:

useEffect(() => {
  editor.replaceBlocks(editor.topLevelBlocks, initialContent)
}, [editor, initialContent]);

@mawnir
Copy link
Author

mawnir commented Jan 31, 2024

@matthewlipski i couldn't make to work with typescript :
Argument of type 'string | undefined' is not assignable to parameter of type 'PartialBlock<BlockSchemaFromSpec...

@matthewlipski
Copy link
Collaborator

matthewlipski commented Feb 12, 2024

Ah ok the problem is more clear then - you should use an array of PartialBlock objects for initialContent instead of a string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants