Sometimes I am really confused that when do I need to append .bind(this)
to the method (ES5) or use arrow function (ES6)
e.g.
handleSubmit(e) {
console.log(this)
}
handleChange(e) {
console.log(this)
}
render() {
return (
<div className="col-sm-4">
<form onChange={e => this.handleChange(e)} onSubmit={e => this.handleSubmit(e)}>
<input type="text" name="user" placeholder="user"/>
<input type="text" name="comment" placeholder="comments"/>
<input type="submit" hidden/>
</form>
</div>
)
}
So if I want to access this
inside handleChange
and handleSubmit
, then I have to use arrow function of onChange
and onSubmit
, otherwise the onChange
and onSubmit
can be changed as:
<form onChange={this.handleChange} onSubmit={this.handleSubmit}>
Am I right? thanks