这是我的 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 改变 content 中的 JSON
onContentStateChange: 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;
那么实际上我应该怎么做呢?