2

I get a Html string from my API : "consigne": "<p>test</p>\n" and I want to display it on my editor.

My Editor is :

<Editor id="consigne" name= "consigne" editorState={consigne} value={draftToHtml(convertToRaw(consigne.getCurrentContent()))} onEditorStateChange={(consigne) => {this.setState({consigne})} localization={{ locale: 'fr' }}/>

I want to insert this consigne on my editor.

How I can push this consigne to my editor ?

3 Answers 3

1

I find it by this :

  this.setState({ 
consigne:EditorState.createWithContent(ContentState.createFromBlockArray(convertFromHTML(plan.consigne))) 
    })
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot for posting, however no longer working for current version "react-draft-wysiwyg": "^1.14.5",
1

For the recent version:

const blocks = convertFromHTML(plan.consigne);
this.setState({ 
  consigne: EditorState.createWithContent(ContentState.createFromBlockArray(blocks.contentBlocks, blocks.entityMap)) 
})

1 Comment

thanks, it works for me but without structuring the html.
0

Approach using functional components:

import { useState } from 'react';
import { EditorState, convertFromHTML } from 'draft-js';
import { ContentState } from 'react-draft-wysiwyg';

const blocks = convertFromHTML(htmlData);
const [editorState, setEditorState] = useState(() =>
     EditorState.createWithContent(ContentState.createFromBlockArray(blocks.contentBlocks, blocks.entityMap))
);

1 Comment

Please edit your post to explain your code. Code only answers are unhelpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.