Lexical editor not refreshing when passing editor content as props - suggested solution #3335
Replies: 4 comments 8 replies
-
I'm using exactly the same approach, but it works for me, not sure if the dispatch command and setEditorState inference each other. ? FYI - my snippet:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the response. This approach works for me too, but only as a
plugin. Did you write this useEffect as part of the lexical component
itself?
Just to be clear, I'm no longer blocked, but wondered if the above would be
helpful to others or if the there was a better way of accomplishing the
outcome.
…On Tue, 8 Nov 2022, 22:18 Shawn Cao, ***@***.***> wrote:
I'm using exactly the same approach, *but it works for me*, not sure if
the dispatch command and setEditorState inference each other. ?
FYI - my snippet:
// change current editor content
useEffect(() => {
if (doc && editor) {
if (!doc.content) {
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, null);
} else {
const newState = editor.parseEditorState(doc.content);
editor.setEditorState(newState);
}
}
}, [doc]);
—
Reply to this email directly, view it on GitHub
<#3335 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOFJ7YVM3MMF2U5NG2NTTPDWHLGRHANCNFSM6AAAAAAR2TDDOA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
In case anyone has this same problem, I think a better solution is to add |
Beta Was this translation helpful? Give feedback.
-
This was driving me crazy and I finally figured out my issue. My lexical editor was inside a parent component that could optionally be an Apparently, changing the parent element after the editor initialized caused the state to essentially be lost and saving the updates wouldn't work. No combination of TLDR; the lexical editor's initial state gets "lost" if it's parent element changes to render a different html element/component/wrapper. |
Beta Was this translation helpful? Give feedback.
-
Challenge
I am writing an editor that changes it's content based on an external user interaction (a user clicks on a row of documents and it loads the respective content). The way I have implemented this is the data is passed as a prop to a component when the user clicks the external document row. However, the Lexical editor content was not refreshing or loading (as initialConfig: editorState is only run once I presume) . I was struggling with this for a while until I saw a similar but unrelated post here #2583.
Note: I used to use Draftjs and this was accomplished by setting a new EditorState i.e. EditorState.createWithContent(newDataPassedAsPropsAndConvertedFromRaw)
Solution
My solution was to write a Lexical React Plugin () that listens for updates from the props, and sets the content using editor.parseEditorState(), editor.setEditorState() and CLEAR_HISTORY_COMMAND.
LexicalEditor.js pseudo code
RefreshContentBasedOnActiveDoc.js pseudo code
Any suggestions
Just getting to grips with Lexical after spending a year on Draftjs. Perhaps there's a better way of implementing the above - happy to hear.
Great work on the library core team.
Beta Was this translation helpful? Give feedback.
All reactions