Skip to content

Instantly share code, notes, and snippets.

@forl
Created May 17, 2016 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forl/92926c8bd19f95db74625485b841a246 to your computer and use it in GitHub Desktop.
Save forl/92926c8bd19f95db74625485b841a246 to your computer and use it in GitHub Desktop.
Autobinding
React.createClass has a built-in magic feature that bound all methods to this automatically for you. This can be a little confusing for JavaScript developers that are not used to this feature in other classes, or it can be confusing when they move from React to other classes.
Therefore we decided not to have this built-in into React's class model. You can still explicitly prebind methods in your constructor if you want.
class Counter extends React.Component {
constructor() {
super();
this.tick = this.tick.bind(this);
}
tick() {
...
}
...
}
However, when we have the future property initializers, there is a neat trick that you can use to accomplish this syntactically:
class Counter extends React.Component {
tick = () => {
...
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment