2
votes

In my project I have integrated the editor from react-draft-wysiwyg. Now I need to test the text editor by loading it with some text data. I tried to follow the documentation and my code currently looks like this:

import React, { Component } from 'react';
import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import './textEditor.css';
import { convertFromRaw } from 'draft-js';


const content = {
    "entityMap":{

    },
    "blocks":[
       {
          "key":"637gr",
          "text":"Initialized from content state.",
          "type":"unstyled",
          "depth":0,
          "inlineStyleRanges":[

          ],
          "entityRanges":[

          ],
          "data":{

          }
       }
    ]
 };
export default class RichTextEditor extends Component {

    constructor(props) {
        super(props);
        const contentState = convertFromRaw(content);
        this.state = {
          contentState,
        }
      }

      onContentStateChange: Function =  (contentState) => {
        this.setState({
          contentState,
        });
      }; 
    render() {
        const { contentState } = this.state;
        return (
            <div>
                <Editor
                    editorClassName={"report-editor"}
                    editorState={this.props.editorState}
                    onEditorStateChange={this.props.onEditorStateChange}
                    onContentStateChange={this.props.onContentStateChange}
                />
            </div>
        )
    }
}

I tried to follow the documentation but the json isn't loading. I tried to understand the workarounds but couldn't as I have never used DraftJS before. Can anyone help me with this?

1
What does it say in the console? I haven't seen anything like onContentStateChange = Function = (contentState) => { ... } before. It seems that you have a bunch of basic React related errors. Try taking a look at some of the many Draft.js codepens out there, such as codepen.io/Kiwka/pen/YNYvyG.mzedeler
onContentStateChange = Function = (contentState) => { ... } it says right in the documentation. (under the section data conversion)Proteeti Prova
Can you provide a link?mzedeler
It was a typo, I replaced the line check it. still not workingProteeti Prova
It seems that @oozywaters has provided an answer for you, but in the future, please remember to include the error message if you experience an error.mzedeler

1 Answers

3
votes

You have to use EditorState.createWithContent to create an editor state based on your content data and pass it to the Editor component, like this:

import React, { Component } from 'react';
import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import './textEditor.css';
import { convertFromRaw, EditorState } from 'draft-js';

const content = { ... };

export default class RichTextEditor extends Component {

    constructor(props) {
        super(props);
        const contentState = convertFromRaw(content);
        const editorState = EditorState.createWithContent(contentState);

        this.state = {
          contentState,
          editorState,
        };
    }

    render() {
        const { editorState } = this.state;
        return (
            <div>
                <Editor
                    editorClassName={"report-editor"}
                    editorState={editorState}
                    onEditorStateChange={this.props.onEditorStateChange}
                    onContentStateChange={this.props.onContentStateChange}
                />
            </div>
        );
    }
}

You can check out a live example over here