JavaScript
React Events: binding or property initializer syntax?

by Lipis

class MyButton extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    console.log('this is:', this);
  }

  // ...
}

class MyButton extends React.Component {
  constructor(props) {
    super(props);
  }

  handleClick = () => {
    console.log('this is:', this);
  }

  // ...
}

More: https://facebook.github.io/react/docs/handling-events.html

Lipis

It's exactly the same thing (e.g. Babel compiles the blue corner to the left one) so there's really no reason not to save some keystrokes and a cluttered ctor.

Vassilis Mastorostergios

Sign in to comment!