As a newbie to the React world, I came across similar issues where I could not edit text area, and struggled with binding. It is worth knowing about controlled and uncontrolled elements when it comes to reactions.
The value of the following uncontrolled textarea field of uncontrolled textarea may be changed due to value
<textarea type="text" value="some value" onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following uncontrolled textarea may be changed due to the use of defaultValue or no value attribute
<textarea type="text" defaultValue="sample" onChange={(event) => this.handleOnChange(event)}></textarea> <textarea type="text" onChange={(event) => this.handleOnChange(event)}></textarea>
The value of the following controlled textarea can be changed due to how the value is mapped to the state, as well as in onChange events
<textarea value={this.state.textareaValue} onChange={(event) => this.handleOnChange(event)}></textarea>
Here is my solution using a different syntax. I prefer auto-bind over manual binding, however, if I hadn’t used {(event) => this.onXXXX(event)} this would make the textarea content unavailable for editing OR event.preventDefault() does not work as expected. I think I still have a lot to learn.
class Editor extends React.Component { constructor(props) { super(props) this.state = { textareaValue: '' } } handleOnChange(event) { this.setState({ textareaValue: event.target.value }) } handleOnSubmit(event) { event.preventDefault(); this.setState({ textareaValue: this.state.textareaValue + ' [Saved on ' + (new Date()).toLocaleString() + ']' }) } render() { return <div> <form onSubmit={(event) => this.handleOnSubmit(event)}> <textarea rows={10} cols={30} value={this.state.textareaValue} onChange={(event) => this.handleOnChange(event)}></textarea> <br/> <input type="submit" value="Save"/> </form> </div> } } ReactDOM.render(<Editor />, document.getElementById("content"));
Library versions
"babel-cli": "6.24.1", "babel-preset-react": "6.24.1" "React & ReactDOM v15.5.4"
Raf
source share