The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

hettlage (136) [Avatar] Offline
#1
I'm a bit puzzled: Don't you have to bind the methods onTitleChange, onDescriptionChange, onCreateTask and toggleForm to this in Listing 2.12?
Will Faurot (8) [Avatar] Offline
#2
Yep, you're 100% correct that handlers like `onStatusChange` and `onTitleChange` need to have `this` bound, but instead of binding in the constructor like this:
this.onTitleChange = this.onTitleChange.bind(this)


we chose to use an early-ish stage ES2015 feature that lets us bind `this` directly in the method definitions:


class TasksPage extends Component {
  constructor(props) {
    // ...
  }

  onTitleChange = e => {  // Where the magic happens
    this.setState({ title: e.target.value });
  };

  onDescriptionChange = e => {
    this.setState({ description: e.target.value });
  };
  // ...
}


Check out #5 on this list https://medium.freecodecamp.com/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56 for a quick description.

Thanks for bringing this up though, I think it's a sign that we may hand wave over this in the book. Making a note to update smilie.