Skip to content

How to save and load editor state from localStorage with React? #1937

Discussion options

You must be logged in to vote

This is what I ended up going with. I'd love for it to work on first render, though I couldn't seem to get that working.

import { useLocalStorage } from 'react-use'
import OnChangePlugin from '@lexical/react/LexicalOnChangePlugin'

function RestoreFromLocalStoragePlugin() {
  const [editor] = useLexicalComposerContext()
  const [serializedEditorState, setSerializedEditorState] = useLocalStorage<
    string | null
  >('my-editor-state-example-key', null)
  const [isFirstRender, setIsFirstRender] = React.useState(true)

  React.useEffect(() => {
    if (isFirstRender) {
      setIsFirstRender(false)

      if (serializedEditorState) {
        const initialEditorState = editor.parseEditorState(

Replies: 7 comments 11 replies

Comment options

You must be logged in to vote
2 replies
@transitive-bullshit
Comment options

@Wallman
Comment options

Comment options

You must be logged in to vote
3 replies
@qt-pixels
Comment options

@milaabl
Comment options

@SukhbatMashbat
Comment options

Answer selected by thegreatercurve
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@ly3xqhl8g9
Comment options

@afreeland
Comment options

@ostaplisovyj
Comment options

@milaabl
Comment options

@ostaplisovyj
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet