0

I am trying to fetch the raw data from the database saved with convertToRaw of Draft JS function and represent the data in the editor instead of createEmpty value.

Note : -

When I send props from parent component, child component gets null value initially unto the data is loaded. How can I check this condition and set state?

vam
  • 492
  • 7
  • 17
  • please include an example of your code. Please include a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – plat123456789 Feb 14 '19 at 02:21
  • ``` import React from 'react'; import ReactDOM from 'react-dom'; import {Editor, EditorState} from 'draft-js'; class MyEditor extends React.Component { constructor(props) { super(props); //Here I want to set editorState with propsData this.state = {editorState: EditorState.createEmpty()}; this.onChange = (editorState) => this.setState({editorState}); } render() { return ( ); } } ReactDOM.render( , document.getElementById('container') ); ``` – vam Feb 14 '19 at 02:31
  • 1
    you should probably edit the code in your question for better readability – plat123456789 Feb 14 '19 at 02:55

1 Answers1

0
  1. MyEditor have a raw state inside the component's props
  2. you should not use createEmpty()
  3. you should use convertFromRaw to convert the raw state to contentState
  4. and then use createWithContent to create a EditorState
  5. done
plat123456789
  • 385
  • 3
  • 12
  • Yeah, I have gone through the documentation. But, like I said the issues are with the null value receiving from props on initial load. `constructor(props) { super(props); this.state = { editorState: props.abstract ? EditorState.createWithContent(convertFromRaw(props.abstract)) : EditorState.createEmpty(decorator) }; }` – vam Feb 14 '19 at 03:33