question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ljosberinncommented, Nov 6, 2020

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

1reaction
awareness481commented, Oct 1, 2020

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 !

Read more comments on GitHub >

github_iconTop Results From Across the Web

ES6 Features for Google Apps Script: Arrow Functions
Just like regular anonymous functions, there are two main parts to arrow functions: the parameters and the statements or expressions that make ...
Read more >
Arrow function expressions - JavaScript - MDN Web Docs
Arrow functions are always unnamed. If the arrow function needs to call itself, use a named function expression instead. You can also assign...
Read more >
Understanding Arrow Functions in JavaScript | DigitalOcean
Arrow functions are a new way to write anonymous function ... A function declaration is a named function written with the function keyword....
Read more >
Arrow Functions in JavaScript - codeburst
However, the Arrow functions can be used for non-Anonymous, i.e. named functions: /* This code sample is from the MDN Docs of Arrow...
Read more >
Arrow Functions: JavaScript ES6 Feature Series (Pt 2) - ITNEXT
As I mentioned briefly above, function expressions, since they're assigned to variables, can omit a name, and be what's known as an 'anonymous...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found