【发布时间】:2020-09-23 22:30:19
【问题描述】:
我已经在 DraftJs 的支持下实现了一个富文本编辑器来 React。
在转换为原始数据并对其进行字符串化后,我将 editorState 数据发送到服务器。这很适合将其渲染到前端。
现在,我想获取这些数据并用这些数据更新 editorState。因此,用户可以使用可用数据编辑文本。
我没有,为什么它不起作用。请帮我。我不知道该怎么办。
我的代码: 导入依赖
import { Editor } from "react-draft-wysiwyg";
import { EditorState, ContentState, convertFromRaw, convertToRaw } from "draft-js";
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
初始状态:
const [editorState, setEditorState] = useState(() =>
EditorState.createEmpty()
);
尝试编辑:
setEvent(current);
const data = JSON.parse(current.description);
// EditorState.createWithContent(convertFromRaw(data));
setEditorState(EditorState.createWithContent(ContentState.createFromText('Hello world')));
// EditorState.createWithContent(convertFromRaw(JSON.parse(current.description)));
// convertToRaw(JSON.parse(editorState.getCurrentContent()));
// const state = current.description;
// As per draft.js docs, I didn't understand the syntax
// convertFromRaw(rawState: state): ContentState
// EditorState.createWithContent(JSON.parse(current.description));
// setEditorState(JSON.parse(current.description));
尝试了所有这些东西。但是,没有将editorState 更新为current.description。
注意:current.description 是一个包含原始对象数据的 JSON 字符串。
【问题讨论】:
-
请添加您对编辑此问题标题的建议。如果您认为这不合适。
标签: javascript reactjs draftjs