使用 createWithContent() 设置初始编辑器状态时遇到问题
“richTextDescription”是存储在数据库中的内容
{"blocks":[{"key":"cipnj","text":"Admore ","type":"header-one","depth":0,"inlineStyleRanges":[{"offset":0,"length":7,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"3ncop","text":"jhdf","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":4,"style":"color-rgb(44,130,201)"}],"entityRanges":[],"data":{}},{"key":"2ra8a","text":"no please work !!","type":"header-five","depth":0,"inlineStyleRanges":[{"offset":0,"length":17,"style":"color-rgb(44,130,201)"},{"offset":0,"length":17,"style":"BOLD"}],"entityRanges":[],"data":{}}],"entityMap":{}}
设置初始使用状态钩子:
const [richTextDescription, setRichTextDescription] = useState(
content.richTextDescription
);
状态改变方法:
const onEditorStateChange = (editorState) => {
setEditorState(editorState);
setRichTextDescription(convertToRaw(editorState.getCurrentContent()));
};
这里使用 createWithContent() 设置编辑器状态。
const [editorState, setEditorState] = useState({
editorState: EditorState.createWithContent(
convertFromRaw(JSON.parse(richTextDescription))
),
});
<Editor
initialEditorState={editorState}
onEditorStateChange={onEditorStateChange}
}}
/>
以下显示输出正常
<div
dangerouslySetInnerHTML={{
__html: draftToHtml(JSON.parse(richTextDescription)),
}}
></div>
但是现在显示应该在编辑器中显示可编辑文本的初始编辑器状态。它显示为空白