GoTeamEpsilon / angular-to-react-redux

Angular to React/Redux, a Guide for Angular v1 Experts Looking to Learn React/Redux
MIT License
107 stars 12 forks source link

starting to fight with basic info edit #66

Closed MatthewVita closed 7 years ago

ghost commented 7 years ago

Matt looked what I pushed earlier to ContactComponent.js. You need to bind within the constructor. So right after super(), put something like this.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 since this 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.

MatthewVita commented 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)
  }
MatthewVita commented 7 years ago

...and here's the big win:

  handleInputChange(event) {
    this.setState({
      [event.target.name]: event.target.value
    })
  }
MatthewVita commented 7 years ago

@ShaneChesnutt you may want to study up on this