vdj4y vdj4y - 1 year ago 273
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"

Answer Source

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'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download