Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: disallow arrow functions for defining stateless functional components #412

Closed
lencioni opened this issue Jan 26, 2016 · 11 comments
Labels

Comments

@lencioni
Copy link
Collaborator

Since arrow functions don't have names, and it is useful for React components to have names, it would be nice if there was a rule that enforced using named functions for stateless functional components and prevented using arrow functions for this case.

Good:

function Thing({ prop, anotherProp }) {
  return <div>{prop}{anotherProp}</div>;
}

Bad:

const Thing = ({ prop, anotherProp }) => {
  return <div>{prop}{anotherProp}</div>;
}
@ljharb
Copy link
Member

ljharb commented Jan 26, 2016

👍

2 similar comments
@justjake
Copy link

👍

@wyattdanger
Copy link

👍

@goatslacker
Copy link

LGTM

@yannickcr
Copy link
Member

Is it really useful since AFAIK Babel will keep compiling both as named components ?

The display-name rule should already prevent you from making "anonymous" components.

@ljharb
Copy link
Member

ljharb commented Jan 27, 2016

@yannickcr this assumes one is always using Babel to compile arrow functions, which is not necessarily the case.

@lencioni
Copy link
Collaborator Author

Is it really useful since AFAIK Babel will keep compiling both as named components ?

Ah, interesting. I tried it out in the REPL and you are correct about Babel compiling both as named functions.

However, in addition to what @ljharb mentioned, in the project I am working in (which uses Babel), when I look in the React inspector I see components that are arrow functions called <StatelessComponent> and components that are named functions called by the name of the function. I checked out the transpiled code and, at least with the version and configuration we are using, the arrow function is transpiled to an anonymous function.

Even if this were not the case, I can imagine that some people would simply want to enforce consistency of always using either named functions or arrow functions across their codebase as a stylistic choice.

@yannickcr
Copy link
Member

@ljharb you're right, you may even not need a transpiler at all if you're not using JSX.

@lencioni maybe a difference between Babel 5 and 6 ? The REPL is still using Babel 5. The <StatelessComponent> case is exactly the sort of things display-name should prevent. But maybe there is some case where the acceptTranspilerName option is assuming that Babel will assign a name when in reality this is not the case. I will do some tests.

But yes, it makes sense to have a rule to enforce consistency for stateless components.

@tleunen
Copy link

tleunen commented Mar 17, 2016

Currently, arrow functions have names if they are assigned to a variable. They automatically take the name of the variable. It's currently the case in Canary for example, so I searched if it's something that will change in the future but couldn't find anything. I found this article showing each case though http://www.2ality.com/2015/09/function-names-es6.html.

So even without using Babel, it seems the only bad case is to export the arrow function without assignment.

@golopot
Copy link
Contributor

golopot commented Jun 4, 2022

This is handled by #2414

@ljharb
Copy link
Member

ljharb commented Aug 5, 2022

Indeed, this is now handled by the function-component-definition rule.

@ljharb ljharb closed this as completed Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

8 participants