Search code examples
javascriptreactjsdraftjs

Get the text typed from a user in draftjs


I have a form on my react app. I want to capture the input from the form from my users and store the text in a state property and then send the text back to my server. I have followed the basic DraftJS tutorial however it gives me a map in my state, instead of just the text needed to send back to the server.

 constructor(props) {
        super(props);
        this.state = {
            name: '',
            teamName: '',
            editorState: EditorState.createEmpty(),
            teamId: '',
            uploadedFileCloudinaryUrl: ''
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
        this.onChange = (editorState) => this.setState({ editorState });
    }

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

Is there something special I have to do to get the text from the editor?


Solution

  • It appears that you want to send an object to the server and your server only accepts strings. In that case you can use JSON.stringify. So to send the editor state to the server you would run sendToSeverFunction(JSON.stringify(this.props.editorState)) and then decode the string in your server.