这是我的editor.js
我在const内容中有示例JSON数据。 我想要做的是,最初当我打开我的编辑器时,它应该在变量内容中呈现初始内容。 但我不知道如何更新editorState,因为它是不可变的。
import React, { Component } from 'react';
import { EditorState, convertToRaw, convertFromRaw, ContentState } from 'draft-js';
import { Editor } from 'react-draft-wysiwyg';
const content = {"blocks":[{"key":"5ngeh","text":"hi there !!!!!","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}],"entityMap":{}};
这是我的BlogEdit组件:
class BlogEdit extends Component {
constructor(props) {
super(props);
const contentState = convertFromRaw(content);
console.log(contentState);
this.state = {
editorState: EditorState.createEmpty(), //I need to change this to actually render the content of content variable in editor
contentState,
}
console.log(contentState);
}
此函数负责根据editorState
更改内容中的JSONonContentStateChange: Function = (contentState) => {
this.setState({
contentState,
});
};
这是渲染部分......
render() {
const { editorState } = this.state;
const { contentState } = this.state;
return (
<div>
<Editor
editorState={editorState}
wrapperClassName="demo-wrapper"
editorClassName="demo-editor"
onContentStateChange={this.onContentStateChange}
/>
</div>
);
}
}
export default BlogEdit;
那么我应该做什么呢?
答案 0 :(得分:1)
编辑器组件的道具名称为 initialContentState 。您可以在此处传递内容状态。
答案 1 :(得分:1)
您可以使用内容
来代替 EditorState.createEmpty() let editorStatewithContent = EditorState.createWithContent(contentState);
//Then you set the state
this.state = {
editorState: editorStatewithContent
}
您可能必须仔细阅读该文档,文档中的所有内容都得到了很好的解释。
答案 2 :(得分:0)
如果您正在使用redux并且您的编辑器状态没有entityMap
对象,它将引发错误。
所以你可以做到
const content = convertFromRaw({...contentState, entityMap: {}})
这还将解决您的invalid RawDraftContentState
无需设置任何initialContentState
,无需设置该值即可实现
this.setState({
editorState: EditorState.createWithContent(content)
})