编辑器启动时就像有空字符串一样!
我尝试过这个:
EditorState.createWithContent(ContentState.createFromText('Hello'))
还有这个:
const html = '<p>Hey this <strong>editor</strong> rocks 😀</p>';
const contentBlock = htmlToDraft(html);
if (contentBlock) {
const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
const editorState = EditorState.createWithContent(contentState);
this.state = {
editorState,
};
}
还有这个:
import htmlToDraft from 'html-to-draftjs'
htmlToDraft(text)
没有任何效果!
最佳答案
您没有显示您正在对内容做什么,但以下(即您的第一个选项)可以正常工作
<Editor
editorState={EditorState.createWithContent(
ContentState.createFromText("Hello")
)}
/>
就像这样:
import {
Editor,
EditorState,
ContentState,
convertFromHTML
} from "draft-js";
...
const blocksFromHTML = convertFromHTML(
"<p>Hey this <strong>editor</strong> rocks 😀</p>"
);
const content = ContentState.createFromBlockArray(
blocksFromHTML.contentBlocks,
blocksFromHTML.entityMap
);
return (
<Editor editorState={EditorState.createWithContent(content)} />
);
关于javascript - 从 HTML 创建的 DraftJS 编辑器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679805/