Super useful Arrow Function in React

To avoid the annoying function binding in react components classes

constructor(props) {
   super(props);
   this.myFuncInClass = this.myFuncInClass.bind(this);
}

Install the following plugin via npm and enable it

babel-plugin-transform-class-properties

Then you can write arrow function and void the binding.

myFuncInClass = () => { 
  // code
}

Be aware about the difference between Arrow function expression and Traditional function expression.

 

Leave a Reply