Here are two examples of writing an ES6 class method:
The first uses non-fat arrow, or concise method syntax--whatever it is correctly called:
class Test extends Component {
handleClick() {
return 'clicked'
}
}
The second uses fat arrow syntax:
class Test2 extends Component {
handleClick = () => {
return 'clicked'
}
}
Should I always prefer to write using fat arrow syntax?
One thing I notice is you could use implicit return which could shorten code.
In most examples I see of people writing constructor()
and render()
methods in React, they do not use fat arrow syntax for those, but they do for all other class methods.
Why is this?
I am guessing the answer will have something to do with the this
keyword since it is fairly intrinsic to classes and the context-preserving nature of fat arrows, but how does this relate to React and fat arrow syntax on class methods? The only case I can think of is that you might be able to avoid binding in the constructor for some cases depending how the class method is called later.
I would appreciate a scientific answer or a list of cases where a distinction is important.