How to clear input field in Draft-js

None of the demos I've seen for Draft-js (Facebook built on React) show how to clear the input field after posting. For example, see This code associated with awesome-draft-js , where the value you submit remains in the input field after submission. There is also no function in the api that seems to be designed for this. What I did for this is to create a new empty state when sending the button, for example,

onSubmit(){ this.setState({ editorState: EditorState.createEmpty(), }) } 

However, since I create an empty state in the constructor when the editor loads like this

  this.state = { editorState: EditorState.createEmpty(), }; 

I am worried that maybe I will not do it right, that is, the previous state object may become a memory leak. Question: what is the supposed way to reset the state in the above situation (i.e. submit button)

+8
reactjs draftjs
source share
1 answer

To clear the state of the editor, it is recommended NOT to use EditorState.createEmpty (), you should use the createEmpty initialization.

The correct way to reset content in the editor is:

 import { EditorState, ContentState } from 'draft-js'; const editorState = EditorState.push(this.state.editorState, ContentState.createFromText('')); this.setState({ editorState }); 

@source: https://github.com/draft-js-plugins/draft-js-plugins/blob/master/FAQ.md

+14
source share

All Articles