首页 > 解决方案 > Draft-js 所见即所得更新 onEditorStateChange 之外的内容状态

问题描述

有没有办法在 onEditorStateChange 之外为编辑器设置内容状态?例如在 useEffect 钩子中。


  const editorFieldContent = EditorState.createWithContent(
    ContentState.createFromBlockArray(convertFromHTML(field.value))
  );

  const [editorState, setEditorState] = useState(editorFieldContent);

  const onEditorChange = (e) => {
    setFieldValue(field.name, 
      draftToHtml(convertToRaw(editorState.getCurrentContent())));

    setEditorState(e);
  };

  useEffect(() => {
    //is there any function in draft js to set content state outside onEditorChange? something like this
    editorState.setContentState(field.value)
  }, [field.value])

  <Editor
    name={field.name}
    editorState={editorState}
    onEditorStateChange={onEditorChange}
  />

标签: reactjsreact-draft-wysiwyg

解决方案


推荐阅读