4

I want to get the content of my editor and eventually store to it to a const content. I'm getting an error of _draftJs.EditorState.getCurrentContent is not a function.

import React from 'react'
import ReactDOM from 'react-dom'
import {Editor, EditorState, RichUtils} from 'draft-js'

const content = EditorState.getCurrentContent()
console.log('str= ', EditorState.getCurrentContent())
console.log('content=', content)
class MyEditor extends React.Component {
  constructor (props) {
    super(props)
    this.state = {editorState: EditorState.createEmpty()}
    this.onChange = (editorState) => this.setState({editorState})
    this.handleKeyCommand = this.handleKeyCommand.bind(this)
  }
  _onBoldClick () {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState,     'BOLD'))
  }
  _onUnderlineClick () {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, 'UNDERLINE'))
  }
  render () { 
    return (
      <div id='content'>
      <h1>Notejs</h1>
      <div id='editor'>
      <button onClick={this._onBoldClick.bind(this)}>Bold</button>
      <button onClick={this._onUnderlineClick.bind(this)}>Underline</button>
      <Editor editorState={this.state.editorState} onChange={this.onChange} />
      </div>
      </div>
    )
  }
}

ReactDOM.render(
  <MyEditor />,
  document.getElementById('app')
)
David Kim
  • 41
  • 1
  • 2

3 Answers3

2

this.state.editorState.getCurrentContent() not EditorState.getCurrentContent()

Jiang YD
  • 3,205
  • 1
  • 14
  • 20
0

import convertToRaw convertToRaw(this.state.editorState.getCurrentContent())

David Kim
  • 41
  • 1
  • 2
0

Lets assume that we have a save button for saving data. Additionally we needs to import convertFromRaw and convertToRaw modules which are provided by draft-js.

import React from 'react'
import ReactDOM from 'react-dom'
import { Editor, EditorState, RichUtils, ContentState, convertFromRaw, convertToRaw} from 'draft-js';

class MyEditor extends React.Component {
  constructor (props) {
    super(props)
    this.state = {editorState: EditorState.createEmpty()}
    this.onChange = (editorState) => this.setState({editorState})
    this.handleKeyCommand = this.handleKeyCommand.bind(this)
  }

  _onBoldClick () {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState,'BOLD'))
  }

  _onUnderlineClick () {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState,'UNDERLINE'))
  }

  _onSave() {

    const contentState = this.state.editorState.getCurrentContent();
    const editorContentRaw = convertToRaw(contentState);

    /* we can save this editorContentRaw inside the DB. */

  }

  render () { 
    return (
      <div id='content'>
        <h1>Notejs</h1>
        <div id='editor'>
          <button onClick={this._onBoldClick.bind(this)}>Bold</button>
          <button onClick={this._onUnderlineClick.bind(this)}>Underline</button>
          <Editor editorState={this.state.editorState} onChange={this.onChange} />
          <button onClick={this._onSave.bind(this)}>Save</button>
        </div>
      </div>
    )
  }
}

ReactDOM.render(
  <MyEditor />,
  document.getElementById('app')
)
Eranga Kapukotuwa
  • 4,542
  • 5
  • 25
  • 30