Created
May 17, 2016 08:06
-
-
Save forl/92926c8bd19f95db74625485b841a246 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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