vdj4y vdj4y - 7 days ago 4x
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({
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"


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'))