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
Comments
👍 |
2 similar comments
👍 |
👍 |
LGTM |
Is it really useful since AFAIK Babel will keep compiling both as named components ? The |
@yannickcr this assumes one is always using Babel to compile arrow functions, which is not necessarily the case. |
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 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. |
@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 But yes, it makes sense to have a rule to enforce consistency for stateless components. |
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. |
This is handled by #2414 |
Indeed, this is now handled by the |
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:
Bad:
The text was updated successfully, but these errors were encountered: