Is it good to use arrow functions for all React component methods?

The components of

React do not use bind (this), all with arrow functions. Is there any disadvantage in this?

Mar.03,2022

The advantage of the

< del > arrow function is that it can automatically bind the this < / del > arrow function in the context. The advantage is that it will not create its own this , it will only inherit this from the upper layer of its scope chain. Of course, this may also be a disadvantage, for example, when you need to change this , you need to change it to a normal function.
another situation I encounter is the need to use decorators, ordinary functions use decorators to directly override descriptor.value . But if decorated in the arrow function above descriptor.value is empty, need to operate through other methods, a little trouble, and finally I directly changed to a normal function, in the outermost layer binding this decorator to solve.
actually not much to entangle, as long as you understand what circumstances can not be used, other cases boldly use. Whenever you have a problem, keep it in mind. It's just that you can't use it. It's the same as whether or not to add a semicolon at the end. Basically all the situations that can be encountered after using too much have been encountered. As for the situations that have not been encountered, we will talk about them when we encounter them.

< hr >

Arrow function expression


it is recommended to use the arrow function, which can avoid the hidden problems caused by this pointing. The second floor said that "the advantage of the arrow function is that it can automatically bind the this in the context". It is not bound by yourself, but the arrow function does not automatically change the this, at all, that is, there is no this, in the arrow function. The this in the arrow function is one level above it.


has no shortcomings, it is recommended to write this in itself.

I have written a lot of projects, but I haven't found that I have to use bind (this)

.

has its drawbacks, of course, the arrow function does not have a name and sometimes cannot provide friendly stack information.

if you write with the arrow function, you may see piles of anonymous in the error stack

Menu