lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: Unable to find an active editor state. State helpers or node methods can only be used synchronously during the callback of editor.update() or editorState.read().

Open muhammadmudasir1 opened this issue 10 months ago • 14 comments

i create reactjs app which works fine but after i use react.lazy at multiple location not in editor and create a build it give the error Unable to find an active editor state. State helpers or node methods can only be used synchronously during the callback of editor.update() or editorState.read(). at getActiveEditorState (http://localhost:3000/static/js/bundle.js:755073:13) at $getRoot (http://localhost:3000/static/js/bundle.js:748462:26) at $rootTextContent (http://localhost:3000/static/js/bundle.js:747214:65) at $isRootTextContentEmpty (http://localhost:3000/static/js/bundle.js:747236:14) at $canShowPlaceholder (http://localhost:3000/static/js/bundle.js:747268:8) at http://localhost:3000/static/js/bundle.js:747308:16 at readEditorState (http://localhost:3000/static/js/bundle.js:740294:12) at EditorState.read (http://localhost:3000/static/js/bundle.js:741394:12) at canShowPlaceholderFromCurrentEditorState (http://localhost:3000/static/js/bundle.js:723705:61) at http://localhost:3000/static/js/bundle.js:723709:109

muhammadmudasir1 avatar Apr 21 '24 18:04 muhammadmudasir1

This can happen if you have imported multiple versions of Lexical, unfortunately the callstack isn't enough information to help you with this issue. Can you reproduce this error and post the reproduction to github, stackblitz, or codesandbox so it can be inspected by someone else?

etrepum avatar Apr 22 '24 01:04 etrepum

Ref this, I suddenly started getting this same error on some occassions... It works fine in storybook ( with the same data). But when using in application it gives this error : Uncaught Error: Unable to find an active editor. This method can only be used synchronously during the callback of editor.update(). I´ve made sure that we use only one version of lexical. Any ideas on what to try? How to make sure that editor has value?! Only change we´ve made is converting to Vite. Anything there that should be mentioned to remember?

caskEqui avatar Apr 29 '24 11:04 caskEqui

Make sure you are only using import or only using require. Mixing both could cause problems. Are you using any third party packages that depend on lexical? Can you post a small project that reproduces this error?

Lexical is tested with vite, so as long as there isn't strange configuration that wouldn't cause a problem on its own.

etrepum avatar Apr 29 '24 13:04 etrepum

I've encoutred the same issue, trying in a monorepo projects with a wbpackk (using Create React App)

marwencherif avatar Jul 02 '24 15:07 marwencherif

This issue is almost exclusively caused by mixing multiple versions of lexical or its dependencies in the same project. All lexical packages must be the exact same version.

etrepum avatar Jul 02 '24 16:07 etrepum

Hey @etrepum, I hope you are well!

What do you mean "almost"?

I'm also experiencing this issue and still haven't realized what's happening. I'm trying to update lexical libraries from version 0.12.5 (which works fine) to 0.16.0. I'm not mixing imports/require, I only use imports in my project. I tried some versions between these, and it worked fine until the 0.14.1.

It seems the error comes from either the RichTextPlugin or PlainTextPlugin. Here's the code:


import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary';
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
...
<RichTextPlugin
    contentEditable={<ContentEditable />}
    placeholder={<div>{placeholder}</div>}
    ErrorBoundary={LexicalErrorBoundary}
/>
...

I also checked the lock file, and the only version I can see from Lexical is 0.16.0, so I'm not mixing versions.

rodrigolungui avatar Jul 04 '24 20:07 rodrigolungui

next.js and other build tools often have caches that are not implemented correctly, try removing .next and node_modules and do a fresh install & build so you have a clean environment next time you test it. Other than that, without a more complete picture of what's going on (e.g. a full reproduction of the issue in an environment that I can inspect), I can't provide any more specific suggestions.

etrepum avatar Jul 05 '24 00:07 etrepum

The problem is that Lexical.dev.js and Lexical.dev.mjs are used simultaneously in the Lexical package, causing editor state exceptions.

The use of Lexical.dev.js is caused by the @lexical/react/LexicalComposer package.

The webpack v4 does not support the use of the exports field in package.json.

image

image

resolve: {
    alias: {
      '@lexical/react/LexicalComposer': '@lexical/react/LexicalComposer.mjs',
    },
},

Use the above code to fix the reference problem.

Arweil avatar Aug 21 '24 08:08 Arweil

Thanks, Arweil. It helped fix the error for ReachEditor. But the issue persists when I'm trying to add ToolbarPlugin. Could you help me with this, please? I use the code from the official documentation here: https://lexical.dev/docs/getting-started/react#adding-ui-to-control-text-formatting My Webpack version: 4.43.0

nickelaos avatar Aug 21 '24 15:08 nickelaos