Closed MatthewVita closed 7 years ago
@danielehrlich you are the MAN!
so in my ctor, I do:
super()
this.handleInputChange = this.handleInputChange.bind(this)
..then later on I do:
<input type="text" onChange={this.handleInputChange} name="name" />
...which makes this know about this
:
handleInputChange(event) {
const value = event.target.value
const name = event.target.name
console.log(value, name, this.state)
}
...and here's the big win:
handleInputChange(event) {
this.setState({
[event.target.name]: event.target.value
})
}
@ShaneChesnutt you may want to study up on this
Matt looked what I pushed earlier to
ContactComponent.js
. You need to bind within the constructor. So right aftersuper()
, put something likethis.handleInputChange = this.handleInputChange.bind(this)
.The reason you have to do this is because of the changes in lexical binding with ES6. Since
render
won't actually be called in the same scope as the rest of the Class, and sincethis
is evaluated at run time not author time, this can be quite confusing.TL;DR:
this
evaluates to different things depending on its location inside of a React class.