reactjsreact-draft-wysiwyg

how to displayed local content react-draft-wysiwyg


how do i display edited content with all styles?

const content = {
  entityMap: {},
  blocks: [
    {
      key: "637gr",
      text: "Initialized from content state.",
      type: "unstyled",
      depth: 0,
      inlineStyleRanges: [],
      entityRanges: [],
      data: {},
    },
  ],
}

export default class EditorConvertToJSON extends Component {
  constructor(props) {
    super(props)
    const contentState = convertFromRaw(content)
    this.state = {
      contentState,
    }
  }

  onContentStateChange = (contentState) => {
     this.setState({
     contentState,
    })
  }

  render() {
    const { contentState } = this.state
    console.log("==============")
    console.log("contentState", contentState)

return (
  <div>
    <Editor
      wrapperClassName="demo-wrapper"
      editorClassName="demo-editor"
      onContentStateChange={this.onContentStateChange}
      // editorState={this.state.contentState}
    />

    <Editor editorState={contentState} readOnly />
  </div>
)

} }

I get an error TypeError: editorState.getImmutable is not a function where am I wrong? may need to display this data in divs and other html tags? I'm completely confused


Solution

  • I hope this helps you:

    Do

    npm i draftjs-to-html

    const content = {
      entityMap: {},
      blocks: [
        {
          key: "637gr",
          text: "Initialized from content state.",
          type: "unstyled",
          depth: 0,
          inlineStyleRanges: [],
          entityRanges: [],
          data: {},
        },
      ],
    }
    
    export default class EditorExample extends Component {
      constructor(props) {
        super(props)
        this.state = {
          contentState : null
        }
      }
    
      onContentStateChange = contentState => {
       console.log('as HTML:', draftToHtml(contentState));
       this.setState({ contentState});
      }
    
      render() {
       const { contentState } = this.state
       return (
        <Editor
         initialContentState={content}
         editorContent={contentState}
         onContentStateChange={this.onContentStateChange}
         wrapperClassName="demo-wrapper"
         editorClassName="demo-editor"
         />
      )
     }
    }