Draft-js: EditorState.createWithContent causes: "contentState.getBlockMap is not a function" exception

Created on 21 May 2016  路  2Comments  路  Source: facebook/draft-js

Hi,
I'm trying to initialize text editor with some HTML. The problem is that the method "createWithContent" throws an error.
My code:

import {Editor, EditorState,  convertFromHTML} from 'draft-js'
import React, { Component } from 'react'

export default class RTEEditor extends Component {        
    constructor(props) {
        super(props);

        this.state = { editorState: EditorState.createEmpty() }; //createWithContent(convertFromHTML(this.props.initialHtmlContent)) };

        this.onChange = (editorState) => { this.setState({ editorState }); };            
    }

    render() {
        return <div>
                    <Editor editorState={this.state.editorState} onChange={this.onChange} ref="editor" />
               </div>
    }
}
![draftjs2](https://cloud.githubusercontent.com/assets/4653046/15448763/d21efaa2-1f6a-11e6-873b-730a74239df0.JPG)

What I also noticed, when I'm using "createWithContent" RTEEditor component constructor is called twice and first attempt is running without error but second call causes exception.
Using "createEmpty" function does not make two constructor calls and component is displayed properly.

draftjs

Most helpful comment

You need to do the following:

EditorState.createWithContent(
  ContentState.createFromBlockArray(
    convertFromHTML(this.props.initialHtmlContent)
  )
);

All 2 comments

You need to do the following:

EditorState.createWithContent(
  ContentState.createFromBlockArray(
    convertFromHTML(this.props.initialHtmlContent)
  )
);

the answer from mitermayer is the solution. Thank you very much :)

Was this page helpful?
0 / 5 - 0 ratings