foundersandcoders / react-week

🔥 React 💧 Week 🌫
20 stars 5 forks source link

Workshop on `this` #2

Closed oliverjam closed 6 years ago

oliverjam commented 6 years ago

this is confusing, especially when you start writing ES6 classes and have to figure out why you can't call setState of undefined...

We ideally need a morning challenge that introduces the concept of ensuring you know what your this is, and covers at least a few of the common ways you see this bound in React.

  1. Inline arrow functions onClick={() => this.setState({ ... })}
  2. Binding in the constructor this.handleClick = this.handleClick.bind(this) (ugh)
  3. Class properties with arrow functions handleClick = () => this.setState({ ... }) 🙌
ZooeyMiller commented 6 years ago

Gonna work on this this week 👍