Open binaryhq opened 6 years ago
You can fetch and populate your Editor Stat this way :
constructor(props) {
super(props);
const html = however you get your html
let editorState
if (html) {
const contentBlock = htmlToDraft(html)
if (contentBlock) {
const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks)
editorState = EditorState.createWithContent(contentState)
} else {
editorState = EditorState.createEmpty()
}
} else {
editorState = EditorState.createEmpty()
}
this.state = {
editorState,
}
}
And render your editor this way :
render() {
const { editorState } = this.state;
return (
<div>
<Editor
editorStyle={{
padding: '0 10px',
}}
editorState={editorState}
wrapperClassName="demo-wrapper"
editorClassName="demo-editor"
onEditorStateChange={this.onEditorStateChange}
/>
</div>
)
}
@Sashkan thanks for your recommend, but if I want to add an image from parent component, we have to re-render editor again ?
You can fetch and populate your Editor Stat this way :
constructor(props) { super(props); const html = however you get your html let editorState if (html) { const contentBlock = htmlToDraft(html) if (contentBlock) { const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks) editorState = EditorState.createWithContent(contentState) } else { editorState = EditorState.createEmpty() } } else { editorState = EditorState.createEmpty() } this.state = { editorState, } }
And render your editor this way :
render() { const { editorState } = this.state; return ( <div> <Editor editorStyle={{ padding: '0 10px', }} editorState={editorState} wrapperClassName="demo-wrapper" editorClassName="demo-editor" onEditorStateChange={this.onEditorStateChange} /> </div> ) }
This should be posted in the documentation, I was stuck till I found your answer.
Hello geeks.
I have problem here. might be silly, please help componentwillmount will get html content from remote server. How do i use this html as editorState? here is my simplified test code.
I'm i doing anything wrong. Please suggest.
I used Modifier, something like this.
this prints html as unicode text.
please help.