react-email-editor icon indicating copy to clipboard operation
react-email-editor copied to clipboard

Cannot read properties of null (reading 'editor')

Open fedotxxl opened this issue 4 years ago • 4 comments

I've got component which conditionally loads on the page. OnLoad method has following code:

  const onLoad = () => {
    if (props.model) {
      emailEditorRef.current.editor.loadDesign(props.model)
    }

    emailEditorRef.current.editor.addEventListener('design:updated', (e: any) => {
      emailEditorRef.current.editor.exportHtml((data: HtmlExport) => {
        props.handleChange?.({
          json: data.design,
          html: data.html,
        })
      })
    })
  }

When component loads first time it works correctly. On second time I've got an exception: Cannot read properties of null (reading 'editor') in line emailEditorRef.current.editor.addEventListener. I've fixed it by wrapping onLoad in setTimeout.

Can you help with correct solution?

fedotxxl avatar Dec 03 '21 13:12 fedotxxl

I have the same problem

IamNotHuman avatar Dec 28 '21 18:12 IamNotHuman

Could be wrong, but I think this is happening because loadDesign is getting called before editor has been fully rendered.

To avoid this race condition, I think you can use the onReady() prop instead.

asdfryan avatar Feb 23 '22 08:02 asdfryan

In fact, I see now that onLoad() was marked as deprecated in https://github.com/unlayer/react-email-editor/commit/1d03c2ee33397825cd3be9d79030a5832278d44c (presumably in favor of onReady()).

Should we go ahead and update the examples?

asdfryan avatar Feb 23 '22 08:02 asdfryan

onReady instead of onLoad should fix your problem

lucasbesen avatar Mar 24 '22 13:03 lucasbesen