question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

editorState.getCurrentContent is not a function

See original GitHub issue

Hi,

I’m trying to get Draft.js to work with Redux and Reselect in my React project. I’m setting the initial editorState in my reducer and then use it in my component.

import { fromJS } from 'immutable';
import { EditorState } from 'draft-js';
import {
  RECEIVE_EDITOR_CHANGE,
} from './constants';

const initialState = fromJS({
  editorState: EditorState.createEmpty(),
});

function writePageReducer(state = initialState, action) {
  switch (action.type) {
    case RECEIVE_EDITOR_CHANGE:
      return state
        .set('editorState', action.editorState);
    default:
      return state;
  }
}
<Editor editorState={this.props.editorState} onChange={this.props.editorChange} />

When I load the page I always get editorState.getCurrentContent is not a function as an error.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
reme3d2ycommented, Aug 30, 2016

You can save ContentState in redux store with convertToRaw, convertFromRaw.

In your component:

constructor(props) {
  super(props);

  const contentState = convertFromRaw(this.props.contentState);
  this.state.editorState = EditorState.createWithContent(contentState);
}

componentWillUnmount() {
  const contentState = this.state.editorState.getCurrentContent();
  // You can save contentState here or somewhere else.
  // this.props.onSave(convertToRaw(contentState));
}

this.onChange = (editorState) => this.setState({editorState});

<Editor editorState={this.state.editorState} ... onChange={this.onChange} />

1reaction
stopachkacommented, Sep 13, 2016

in your code, if you update

const initialState = fromJS({
  editorState: EditorState.createEmpty(),
});

to

const initialState = Map({
  editorState: EditorState.createEmpty(),
});

That would fix your problem.

As @reme3d2y suggested, you could also use convertFromRaw, if the editorState is coming from the outside

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: editorState.getCurrentContent is not a function
I am trying to console the result from react draft wysiwyg and I am getting editorState.getCurrentContent is not a function.
Read more >
draft-js.EditorState.getCurrentContent JavaScript ... - Tabnine
How to use. getCurrentContent. function. in. EditorState ... If the server does not respond with OK, we do not want to clearEditor.
Read more >
How to use the draft-js.EditorState.forceSelection function in ...
getCurrentContent () // Retrieve current block const selection = editorState.getSelection() const blockKey = selection.getStartKey() let block = content.
Read more >
Building a Rich Text Editor with React and Draft.js, Part 3
componentDidMount : The editor will load an empty EditorState if a note does not exist in our database. If a note does exist...
Read more >
draft-js-export-html - Bountysource
editorState.getCurrentContent is not a function $ 0. Created 3 years ago in sstur/draft-js-utils with 3 comments.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found