-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Don't use "export default () => {}" in docs #23575
Comments
Thanks for filling the issue, @gaearon! Here's an example of this in the docs, for reference, mentioned in the twitter thread: https://www.gatsbyjs.org/docs/adding-react-components/#importing-react-components |
It would also be good to add https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-anonymous-default-export.md to the default lint rule set. |
Hi, I would like to work on this, wondering what's the preferred way to update to: Instead of a) or b)
I'm thinking a would be easier to change, but personally I tend to use b on my apps. |
https://twitter.com/dan_abramov/status/1255230047109222400?s=19 |
Let's go with cc @pvdz |
I don't think option A would be a great idea. Some people tend to replace old-style |
@tomadimitrie This rule will warn about it |
@ackvf yeah, but why use |
Yeah we're also discussing dropping constant arrows altogether as it has the same drawbacks and doesn't really help. So agreed, please change it to |
Version A MyComp.propTypes = {};
MyComp.defaultProps = {};
export default function MyComp() {
console.log(
MyComp.name, // MyComp
MyComp.propTypes, // {}
this, // function MyComp
);
}; Version B const MyComp = () => {
console.log(
MyComp.name, // MyComp
MyComp.propTypes, // {}
this, // undefined
);
};
MyComp.propTypes = {};
MyComp.defaultProps = {};
export default MyComp; I would recommend version B. |
We're using eslint-config-react-app so when facebook/create-react-app#8926 is merged we'll bump the dependency :) |
Arrows have their places, but so do function declarations. |
I would recommend version A. export default function MyComp() {} In typescript arrow function with generic type is not very friendly doc generators like "API extractor" evaluate I can remember typescript error about "private type exported" when the arrow function pattern used and emit declaration is on. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Though, it should also be noted that you can't type a Just some quick type hacking: interface PropTypes {a, b, c}
const Arrow: React.FC<PropTypes> = props => props.children
function Funy(props: PropsWithChildren<PropTypes>): React.ReactElement | null {return props.children}
export type A<T> = Parameters<React.FC<T>>
export type R<T> = ReturnType<React.FC<T>>
interface P { a, b, c }
function Other(...[props, c]: A<P>): R<P> {
return props.children
} Funy.., not so fun. We are inclining towards named const exports instead:
|
could we add a summary in some doc to style guide which notation is preferred or add the eslint plugin (#23575 (comment))? |
We are going to follow suit to CRA (facebook/create-react-app#8177) and deprecate the pattern, codemod the rest to function declaration style, and lock it down with a lint. The FC deprecation will keep the existing cases for the sake of simplicity but disallow new cases. This is a work in progress as the conversion codemod is non-trivial. |
I am using version A in my gatsby site , but on Build i get an error : |
See https://twitter.com/JelteHomminga/status/1255230778428010496.
This is a problem because those functions don't get names, so they're anonymous in React DevTools, stack traces, and upcoming features like Fast Refresh.
The text was updated successfully, but these errors were encountered: