jpuri / react-draft-wysiwyg

A Wysiwyg editor build on top of ReactJS and DraftJS. https://jpuri.github.io/react-draft-wysiwyg
MIT License
6.38k stars 1.16k forks source link

Getting Error: Cannot read properties of undefined (reading 'createFromBlockArray') #1389

Closed MichaHumbel closed 10 months ago

MichaHumbel commented 1 year ago

I'm trying to create an Editor with predefined conente thats HTML.

These are my imports:

import { ContentState, Editor } from 'react-draft-wysiwyg';
import htmlToDraft from 'html-to-draftjs';
import { useEffect, useState } from 'react';
import { EditorState } from 'draft-js';

Versions: "react-draft-wysiwyg": "^1.15.0", "html-to-draftjs": "^1.5.0", "react": "18.2.0, "draft-js": "^0.11.7",

const [editorState, setEditorState] = useState<any>(EditorState.createEmpty());

useEffect(() => {
  const text = '<p>Hello World</p>';
  const { contentBlocks } = htmlToDraft(text);
  const contentState = ContentState.createFromBlockArray(contentBlocks);
  const newState = EditorState.createWithContent(contentState);
  setEditorState(newState);
}, []);

This is my Editor:

<Editor
  editorState={editorState}
  toolbar={{
    options: ['inline', 'list', 'link'],
    inline: {
    options: ['bold', 'italic', 'underline'],
    },
    list: {
    options: ['unordered', 'ordered'],
    },
    }}
    onEditorStateChange={(newState) => {
    setEditorState(newState);
   console.log(newState)
  }}
/>

Then when I open the editor i get the following error: Cannot read properties of undefined (reading 'createFromBlockArray')

What am I doing wrong?

CleanShot 2023-07-05 at 17 29 39