编辑器正在启动,就好像它有空字符串!
我试过这个:
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)
什么都不管用!
发布于 2018-06-06 05:47:00
您没有显示您正在对内容做什么,但以下内容(即您的第一个选项)可以正常工作
<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)} />
);
发布于 2020-01-30 01:38:19
如果您感兴趣,我已经通过函数组件将HTML语言引入DraftJS编辑器。下面是构建在@Grantnz答案之上的代码。
import React, { useState } from 'react'
import { Editor, EditorState, RichUtils, ContentState, convertFromHTML } from 'draft-js'
const DraftJS = () => {
// assert hard-coded HTML string, convert it, bring it in as state
const blocksFromHTML = convertFromHTML(
"<p>Hey this <strong>editor</strong> rocks </p>"
);
const content = ContentState.createFromBlockArray(
blocksFromHTML.contentBlocks,
blocksFromHTML.entityMap
);
const [editorState, setEditorState] = useState(EditorState.createWithContent(content));
// handle changes to the Editor component
const onChange = editorState => {
setEditorState(editorState);
}
// build the Editor component
return (
<div>
<h4 className="green-text">From DraftJS.js</h4>
<Editor
editorState={editorState}
onChange={onChange}
/>
</div>
);
}
export default DraftJS;
希望这能有所帮助!
https://stackoverflow.com/questions/50679805
复制相似问题