【问题标题】:Not able to update state with editorState from server in ReactJs for draftJs无法在 ReactJs 中的服务器上使用 editorState 为 DraftJs 更新状态
【发布时间】: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 字符串。

资源: createwithcontent

reactrocket persisting state

【问题讨论】:

  • 请添加您对编辑此问题标题的建议。如果您认为这不合适。

标签: javascript reactjs draftjs


【解决方案1】:

我已经解决了这个问题。 DraftJs 官方文档有点混乱。 它应该有一些例子。所以,初学者和中级开发者都可以得到它。

我已经这样做了:

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

我希望我的努力能对别人有所帮助,他们不需要像我一样浪费时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多