Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function or fat arrow for a React functional component? [duplicate]

I can't help but wondering if there's any advantage between using plain functions and fat arrows for React functional components

const MyMainComponent = () => (   <main>     <h1>Welcome to my app</h1>   </main> )  function MyMainComponent() {   return (     <main>       <h1>Welcome to my app</h1>     </main>   ) } 

Both work of course perfectly fine but is there a recommended way to write those ? Any argument in favor of one or the other ?

Edit: I am aware of the differences when it comes to plain javascript functions (i.e. context, stack trace, return keyword, etc.) that can have an impact for the kind of use you have for functions. However I am asking the question purely in terms of React components.

like image 204
Dispix Avatar asked Jan 23 '19 15:01

Dispix


People also ask

Should I use arrow function for React component?

With the release of ES6 arrow function syntax, you now can use both regular JavaScript function and arrow function syntax when creating components. I would say that using the arrow function for React components is the right choice for most use cases.

When should I use arrow functions in React?

If you use arrow functions within render , each call to render will create new function objects. If you then pass these functions to child elements via props , optimizations based on PureComponent or shouldComponentUpdate will fail (as the arrow function props will change on every render).

Is it good to use arrow functions in render method?

Is it OK to use arrow functions in render methods? Generally speaking, yes, it is OK, and it is often the easiest way to pass parameters to callback functions. If you do have performance issues, by all means, optimize!

What is the use of fat arrow function?

Arrow functions allow us to use the fat arrow => operator to quickly define JavaScript functions, with or without parameters. We are able to omit the curly braces and the function and return keywords when creating a new JavaScript function to write shorter function syntax.


2 Answers

There is no practical difference.

An arrow allows to skip return keyword, but cannot benefit from hoisting. This results in less verbose output with ES6 target but more verbose output when transpiled to ES5 , because a function is assigned to a variable:

var MyMainComponent = function MyMainComponent() {   return React.createElement(     "main",     null,     React.createElement("h1", null, "Welcome to my app")   ); }; 

The overhead is 6 bytes in minified and not gzipped JS file, this consideration can be generally ignored.

Verbosity is not necessarily the case when an arrow is exported, due to optimizations:

var MyMainComponent = (exports.MyMainComponent = function MyMainComponent() {   return React.createElement(     "main",     null,     React.createElement("h1", null, "Welcome to my app")   ); }); 
like image 169
Estus Flask Avatar answered Oct 05 '22 19:10

Estus Flask


Mostly a matter of preference. However, there are some (minor, almost insignificant) differences:

  • The fat arrow syntax lets you omit the curly braces and the return keyword if you return the JSX directly, without any prior expressions. With ES5 functions, you must have the { return ... }.

  • The fat arrow syntax does not create a new context of this, whereas ES5 functions do. This can be useful when you want this inside the function to reference the React component or when you want to skip the this.foo = this.foo.bind(this); step.

There are more differences between them, but they are rarely relative when coding in React (e.g using arguments, new, etc).

On a personal note, I use the fat arrow syntax whenever possible, as I prefer that syntax.

like image 28
Chris Avatar answered Oct 05 '22 19:10

Chris