无法从ReactJs的服务器中使用draftJs的editorState更新状态

问题描述

我已经在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字符串。

资源: createwithcontent

reactrocket persisting state

解决方法

我已经解决了问题。官方的DraftJs文档有点令人困惑。 它应该有一些例子。因此,初学者和中级开发人员都可以得到它。

我已经这样做了:

setEditorState(EditorState.createWithContent(convertFromRaw(JSON.parse(current.description))));

我希望我的努力对其他人有帮助,他们不必像我一样浪费时间。