这是我的editor.js
我在const内容中有示例JSON数据。我想要做的是,当我最初打开编辑器时,它应该在变量content中呈现初始内容。但是我不知道如何更新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更改内容中的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;那么我到底应该做什么呢?
发布于 2018-06-17 12:06:33
编辑器组件有一个属性名称initialContentState.你可以在这里传递contentstate。
发布于 2018-06-28 00:05:31
您可以使用内容创建而不是EditorState.createEmpty(),,
let editorStatewithContent = EditorState.createWithContent(contentState);
//Then you set the state
this.state = {
editorState: editorStatewithContent
}你可能得通读一下这份文件,它把所有的事情都解释得很清楚。
发布于 2020-06-06 06:53:55
如果你正在使用redux,并且你的编辑器状态没有entityMap对象,它将抛出一个错误。
所以你可以这样做
const content = convertFromRaw({...contentState, entityMap: {}})它还将解决您的invalid RawDraftContentState问题
无需设置任何initialContentState,无需设置该值即可实现
this.setState({
editorState: EditorState.createWithContent(content)
})https://stackoverflow.com/questions/50434214
复制相似问题