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: Warn against using an arrow function/anon fn when declaring a component #690
Comments
Perhaps it could be more generic and be configurable to require/allow specific function kinds for SFCs. |
Why? Per spec, the name will be assigned, so it’s fine. |
@gaearon but in practice it is not always assigned. In general, explicit > implicit, and a linter rule to require explicit names by disallowing arrow functions is appropriate. |
Can you help me understand in which situations it wouldn’t get assigned? |
@gaearon In the Babel repl, this function has no name: export default (props) => {
return <div {...props} />;
} which thus would necessitate: const Foo = (props) => {
return <div {...props} />;
};
export default Foo; but export-defaulting one thing only from a file inline is also a best practice according to the Airbnb styleguide. In node 4 and later, if you do |
+1 from me. Reasons:
|
+1, I got caught by the Jest bug pointed out by bkozera, had to rewrite components to not use the fat arrow |
Since otherwise it's an anonymous function where the name has to be inferred, which doesn't work in all cases: https://github.com/airbnb/javascript/blob/master/react/README.md#class-vs-reactcreateclass-vs-stateless Unfortunately there is not yet an ESLint rule to enforce this: jsx-eslint/eslint-plugin-react#690
Since otherwise it's an anonymous function where the name has to be inferred, which doesn't work in all cases: https://github.com/airbnb/javascript/blob/master/react/README.md#class-vs-reactcreateclass-vs-stateless Unfortunately there is not yet an ESLint rule to enforce this: jsx-eslint/eslint-plugin-react#690
…pe for function components Fixes #jsx-eslint#690.
…pe for function components Fixes #jsx-eslint#690.
…pe for function components Fixes #jsx-eslint#690. bla
…pe for function components Fixes jsx-eslint#690.
Component should be named, and arrow functions are anonymous functions, so they produce unnamed components.
Even if Babel currently transforms an anonymous function into a named function, and even if Chrome currently set a name (
fn.name
) when assigning it to a variable, I believe it would be best to add a rule to warn against the use of arrow functions.For example, when using jsdom and enzyme to test components built with arrow functions, we can get errors messages like this one because the component name was not set:
expected the node in <??? /> to have...
.So having a specific rule rule would help waning against arrow and anonymous function in general would help for this.
The text was updated successfully, but these errors were encountered: