【问题标题】:DraftJS persisting data and controlling the data in render blockDraftJS 持久化数据并控制渲染块中的​​数据
【发布时间】:2019-02-14 10:54:08
【问题描述】:

我正在尝试从使用 Draft JS 函数的 convertToRaw 保存的数据库中获取 raw data,并在 editor 中表示 data 而不是 createEmpty 值。

注意:-

当我从父组件发送道具时,子组件最初获取空值以加载数据。如何检查此条件并设置状态?

【问题讨论】:

  • 请包含您的代码示例。请包括Minimal, Complete, and Verifiable example
  • ``` 从'react'导入反应;从 'react-dom' 导入 ReactDOM;从“draft-js”导入 {Editor, EditorState};类 MyEditor 扩展 React.Component { constructor(props) { super(props); //这里我想用propsData设置editorState this.state = {editorState: EditorState.createEmpty()}; this.onChange = (editorState) => this.setState({editorState}); } render() { return ( ); } } ReactDOM.render( , document.getElementById('container') ); ```
  • 您应该编辑问题中的代码以获得更好的可读性

标签: reactjs draftjs


【解决方案1】:
  1. MyEditor 在组件的 props 中有一个原始状态
  2. 你不应该使用 createEmpty()
  3. 您应该使用 convertFromRaw 将原始状态转换为 contentState
  4. 然后使用createWithContent创建一个EditorState
  5. 完成

【讨论】:

  • 是的,我已经浏览了文档。但是,就像我说的那样,问题在于初始加载时从道具接收的空值。 constructor(props) { super(props); this.state = { editorState: props.abstract ? EditorState.createWithContent(convertFromRaw(props.abstract)) : EditorState.createEmpty(decorator) }; }
猜你喜欢
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多