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

[Feature](Docs) Give names to anonymous arrow functions in docs examples #17200

Closed
awareness481 opened this issue Sep 18, 2020 · 8 comments · Fixed by #17510
Closed

[Feature](Docs) Give names to anonymous arrow functions in docs examples #17200

awareness481 opened this issue Sep 18, 2020 · 8 comments · Fixed by #17510
Labels
good first issue Easy to fix issues, good for newcomers

Comments

@awareness481
Copy link
Contributor

For example, in api-routes

we have this example

export default (req, res) => {
  res.statusCode = 200
  res.setHeader('Content-Type', 'application/json')
  res.end(JSON.stringify({ name: 'John Doe' }))
}

Exporting anonymous functions is arguably bad practice. If this agreed upon, I think any examples containing anonymous exports should be changed to use named functions

@timneutkens timneutkens added the good first issue Easy to fix issues, good for newcomers label Sep 21, 2020
@timneutkens
Copy link
Member

Feel free to send a PR.

@Jashnm
Copy link
Contributor

Jashnm commented Sep 30, 2020

Hey, if this issue is not assigned to anyone, can I take this up?
If yes, should I use arrow functions or regular functions?

@timneutkens
Copy link
Member

timneutkens commented Sep 30, 2020

For sure @Jashnm, regular functions is fine!

@awareness481
Copy link
Contributor Author

Obviously the PR is going to be reviewed by one of the Next.js maintainers but I wanted to say that it looks good to me. Nice contribution @Jashnm !

@lcswillems
Copy link

lcswillems commented Oct 28, 2020

Why is exporting anonymous functions a bad practice? @awareness481

@ljosberinn
Copy link
Contributor

@lcswillems

  • named exports are better for treeshaking
  • the barrier of renaming named exports is larger than for default exports, but even then, a named export is find-and-replace-able:
import something from 'somewhere' // Something is internally called foo though
import { foo as something } from 'somewhere' // can find-replace foo easily here

this goes hand in hand with better refactoring support in editors

  • named exports usually have better autocompletion support for editors (although this is changing)

Personally, I consider default exports to be bad practice because of mentioned reasons and actively lint against them. In places where I have to use default exports, e.g. nextjs pages, I // eslint-disable-next-line import/no-default-export.

@lcswillems
Copy link

Thank you for your answer @ljosberinn !

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Easy to fix issues, good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants