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.

Setting new editor state breaks editor

See original GitHub issue

I have this simple example that extends the example a little:

export default class SimpleEmojiEditor extends Component {
  state = {
    editorState: createEditorStateWithText('Test 1')
  };

  render() {
    return (
      <div>
        <div className='editor'>
          <Editor
            editorState={this.state.editorState}
            onChange={editorState => this.setState({ editorState })}
            plugins={[createEmojiPlugin()]}
          />
        </div>
        <button onClick={() => this.setState({ editorState: createEditorStateWithText('Test 2') })}>
          Click
        </button>
      </div>
    );
  }
}

The editor state starts with createEditorStateWithText('Test 1') when the component is mounted. I also have a button, that if I click it, it sets the editor state to createEditorStateWithText('Test 2'). After setting the editor state on the button click, I am no longer able to use emojis in the textarea, but don’t have any errors.

Is this expected, and is there another way I should be ‘updating’ the editor state with a custom value?

My use case is to update the editor’s value on componentDidUpdate from a prop change.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

31reactions
zhouzicommented, Jun 29, 2016

@superKalo DraftJS is built to face complex situations so updating an Editor’s content may not be as trivial as expected. Here is an out of context example of how it could look:

import React, { Component } from 'react';
import { ContentState, EditorState } from 'draft-js';

export default class SimpleEmojiEditor extends Component {
  state = {
    editorState: EditorState.createEmpty()
  };

  setEditorContent (text) {
    const contentState = ContentState.createFromText(text);
    const editorState = EditorState.push(this.state.editorState, contentState);
    this.setState({ editorState });  
  }

  render() {
    return (
      <div>
        <div className='editor'>
          <Editor
            editorState={this.state.editorState}
            onChange={editorState => this.setState({ editorState })}
            plugins={[createEmojiPlugin()]}
          />
        </div>
        <button onClick={() => this.setEditorContent('Test 2')}>
          Click
        </button>
      </div>
    );
  }
}

This is just an example and will not work as is but hopefully it gives you an overview of the steps you’ll have to go through. Have a look at DraftJS’ reference to learn about its internals: EditorState, ContentState.

15reactions
DerKobecommented, Jul 21, 2016

I had a similar problem and the comments guided me to the solution. I wanted to update the editor from raw JSON which comes from an API. So this is what worked for me:

componentWillReceiveProps(newProps) {
  const newContentState = convertFromRaw(JSON.parse(newProps.text))
  const editorState = EditorState.push(this.state.editorState, newContentState)
  this.setState({editorState})
}

I think the gist of this is (like @nikgraf said): always use EditorState.pushand never set the state directly after its initialization.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EditorState - Draft.js
Returns a new EditorState object with the selection at the end. Moves selection to the end of the editor without forcing focus. moveFocusToEnd...
Read more >
How to create in draft-js a new EditorState from a SelectionState
But I cannot figure out how to get from the SelectionState, that I get from editorState.getSelection() to a new EditorState.
Read more >
Building an editor with Draft.js and React - Big Bite Creative
The EditorState object represents the entirety of the state of the editor, including contents, cursor and selection etc.
Read more >
Editor - Tiptap
Commands are used to change the state of editor (content, selection, and so on) and only return ... getText() // Add two line...
Read more >
Reference manual - ProseMirror
ProseMirror keeps all editor state (the things, basically, that would be required to create an editor just like the current one) in a...
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