vdj4y vdj4y - 1 month ago 18
React JSX Question

draftjs how to initiate an editor with content

Stumbled on this cool text editor, draft.js by facebook.
I try to follow the example in github but I want to create an editor with content instead an empty editor.

var EditorState = Draft.EditorState;

var RichEditor = React.createClass({
getInitialState(){
return {editorState: EditorState.createWithContent("Hello")}
//the example use this code to createEmpty editor
// return ({editorState: EditorState.createEmpty()})
}
});


run it, but I get error saying that "Uncaught TypeError: contentState.getBlockMap is not a function"

Answer

The first argument to EditorState.createWithContent is a ContentState, not a string. You need to import ContentState

var EditorState = Draft.EditorState;
var ContentState = Draft.ContentState;

Use ContentState.createFromText And pass the result to EditorState.createWithContent.

return {
  editorState: EditorState.createWithContent(ContentState.createFromText('Hello'))
};
Comments