DEV Community

Discussion on: Arrow Functions + This keyword

Collapse
 
dance2die profile image
Sung M. Kim

But what about you? Why do you like or dislike this method?

I love 🏹 functions.

If you happen to learn React, you'd use arrow functions heavily

because

many components you build would require you to use ES6 "class" (until next release v16.7 alleviate the pain using what's called React Hooks).

In react, each Component (extending React.Component) has an access to setState method to update internal state.

If you don't use an arrow function, methods can't access Component's this.setState thus need to bind this in the constructor.

class App extends React.Component {
  constructor () {
    this.onChange = this.onChange.bind(this);
  }
  ...

  onChange() {
    this.setState({...});
  }
}

Suppose that your component is actually a form with many input elements with many event handlers.
Binding each method in the constructor is both error-prone & very tedious.

Collapse
 
veronicorn profile image
Veronica

Wow! This is awesome, Sung! My boot camp will be starting to learn React soon, which we're all excited about, so I'm even more excited now to know that I'll get to know arrow functions more intimately :)

Collapse
 
dance2die profile image
Sung M. Kim

Woohoo 😸.

Perfect time to learn about the arrow function as it will come in very handy for React ⚛.

And please disregard React Hooks initially, as it'd confuse you if you are just getting started with React