问题是您在此处为每个更改设置了一个新EditorState
的:
useEffect(() => {
if (htmlContent) {
let convertedToHTML = decodeURIComponent(htmlContent);
const blocksFromHtml = htmlToDraft(convertedToHTML);
const { contentBlocks, entityMap } = blocksFromHtml;
const contentState = ContentState.createFromBlockArray(
contentBlocks,
entityMap
);
setEditorState(EditorState.createWithContent(contentState));
}
}, [htmlContent]);
htmlContent
当您通过函数将每次更改转换EditorState
为 HTML 时,它总是在变化。getContent
如果你想用 初始化你EditorState
的,htmlContent
你可以在useState
函数中执行它并删除useEffect
:
const [editorState, setEditorState] = useState(() => {
if (htmlContent) {
let convertedToHTML = decodeURIComponent(htmlContent);
const blocksFromHtml = htmlToDraft(convertedToHTML);
const { contentBlocks, entityMap } = blocksFromHtml;
const contentState = ContentState.createFromBlockArray(
contentBlocks,
entityMap
);
return EditorState.createWithContent(contentState);
}
return EditorState.createEmpty()
});