Setting new editor state breaks editor
See original GitHub issueI 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:
- Created 7 years ago
- Comments:10 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
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.
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:
I think the gist of this is (like @nikgraf said): always use
EditorState.push
and never set the state directly after its initialization.