Next.js: [Feature](Docs) Give names to anonymous arrow functions in docs examples

Created on 18 Sep 2020  路  7Comments  路  Source: vercel/next.js

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

good first issue

All 7 comments

Feel free to send a PR.

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

For sure @jashnm, regular functions is fine!

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 !

Why is exporting anonymous functions a bad practice? @awareness481

@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.

Thank you for your answer @ljosberinn !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YarivGilad picture YarivGilad  路  3Comments

sospedra picture sospedra  路  3Comments

kenji4569 picture kenji4569  路  3Comments

pie6k picture pie6k  路  3Comments

rauchg picture rauchg  路  3Comments