5

I have a property product_des on my database where all the content of react draft wysiwyg store. I want to set the default value on my editor from the database.

//State
    const [onEditorStateChange, setOnEditorStateChange] = useState<any>()
    const [content, setContent] = useState<any>()

<Editor
    editorState={onEditorStateChange}
    toolbarClassName="toolbarClassName"
    wrapperClassName="wrapperClassName"
    editorClassName="editorClassName"

    onEditorStateChange={newState => {
        setOnEditorStateChange(newState)
        setContent(draftToHtml(convertToRaw(newState.getCurrentContent())))
    }}
/>

Here is my database example:

{
_id: "6231a09c0a292231f0bbd16b",
title: "Controller",
reg_price: "499",

product_des: "<p>PS5</p>
"
}
Jerin
  • 717
  • 1
  • 7
  • 28

3 Answers3

8

To achieve that, you need to do following:

import React, { Component } from 'react'
import { EditorState, ContentState, convertFromHTML } from 'draft-js'
import { Editor } from 'react-draft-wysiwyg'

class MyEditor extends Component {
  constructor(props) {
    super(props)
    this.state = {
      editorState: EditorState.createWithContent(
        ContentState.createFromBlockArray(
          convertFromHTML('<p>My initial content.</p>')
        )
      ),
    }
  }

  render() {
    return <Editor editorState={this.state.editorState} />
  }
}

export default MyEditor

PS: Based on Package or ReactJS version answer may differ.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Harsh Doshi
  • 191
  • 11
1

Method 1:- From HTML to react-wyswyg initialValue

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

const Editor =({initialData}) =>  {
  const [editorState, setEditorState] = useState(() =>
    EditorState.createWithContent(
      ContentState.createFromBlockArray(
        convertFromHTML('<p>My initial content.</p>')
      )
    )
  );

  return <Editor editorState={editorState} onChange={setEditorState} />
}

export default Component

Method 2:- From BlockArray(default way in which draft-js supplies changed value) to react-wyswyg initialValue.

Lets assume you get Block array value from your DB. (Stored in Stringfy format)

const rawValue = `{"blocks":[{"key":"e4brl","text":"Hello world","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":11,"style":"BOLD"},{"offset":28,"length":29,"style":"BOLD"},{"offset":12,"length":15,"style":"ITALIC"},{"offset":28,"length":28,"style":"ITALIC"}],"entityRanges":[],"data":{}},{"key":"3bflg","text":"This is example text","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}],"entityMap":{}}`
import React, { Component } from 'react'
import { EditorState, convertFromRaw } from 'draft-js'
import { Editor } from 'react-draft-wysiwyg'

const Editor =({initialData}) =>  {
  const [editorState, setEditorState] = useState(() =>
    EditorState.createWithContent(convertFromRaw(JSON.parse(rawJsText)))
  );

  return <Editor editorState={editorState} onChange={setEditorState} />
}

export default Component
Bilal
  • 11
  • 3
-1

For component function.

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

const [editorState, setEditorState] = useState(() => EditorState.createEmpty());

setEditorState(EditorState.createWithContent(ContentState.createFromBlockArray(convertFromHTML(<p>my text</p>))));