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.

`react/function-component-definition` configuration violates style guide

See original GitHub issue

In eslint-config-airbnb@19.0.0 (specifically https://github.com/airbnb/javascript/commit/1bc8cabd447ef9ce69d27ae70e67eb47daeffc21), react/function-component-definition was configured to allow only function expressions for named components:

https://github.com/airbnb/javascript/blob/1bc8cabd447ef9ce69d27ae70e67eb47daeffc21/packages/eslint-config-airbnb/rules/react.js#L526-L531

This has the effect of disallowing function declarations:

function Foo() {
  return <div />
}

While allowing only this style:

const Foo = function () {
  return <div />
}

This seems like a configuration mistake, as the React style guide consistently uses function declarations for components (as does the ecosystem as a whole) and specifically points out that “relying on function name inference is discouraged”. Let me know if you agree and I’ll PR a fix.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:36
  • Comments:29 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
ljharbcommented, Dec 18, 2021

@slifty yes, it will be corrected soon. btw that seems like a lot of people going to the wrong place; stack overflow isn’t the place to report problems with any software. As far as this issue can tell, there’s only 12 people, not thousands.

7reactions
ljharbcommented, Jan 9, 2022

re the changelog, I’ll try to do a better job highlighting breaking changes in future releases.

Why? For a number of reasons:

  • debugging. arrow functions can never have explicit names, only implicit ones via ES6 name inference, which can be unintuitive and surprisingly missing at times. Consider when you have export const Foo = (props) => {}; and then refactor that to be export const Foo = React.memo((props) => {}) - your “Foo” name suddenly vanishes, because you were relying on inference, probably without realizing it.
  • conceptual mismatch. arrow functions are conceptually meant to replace inline callbacks. They are not a general-purpose function replacement, even though they obviously can be used as such. An arrow function doesn’t properly convey the importance/noun-ness of a react component.

See also:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Struggling with TypeScript, React, Eslint and simple Arrow ...
With this, eslint will ONLY accept arrow functions for Components, ... this means your project won't be using the latest airbnb styleguide, ...
Read more >
airbnb react/function-component-definition - You.com | The AI ...
This seems like a configuration mistake, as the React style guide consistently uses function declarations for components (as does the ecosystem as a...
Read more >
Design Principles - React
Components are often described as “just functions” but in our view they need to be more than that to be useful. In React,...
Read more >
Redux Style Guide
It should be passed to a React component tree via <Provider> , or referenced indirectly via middleware such as thunks. In rare cases,...
Read more >
The last guide to the useEffect Hook you'll ever need
This also means that if there is no second execution of an effect scheduled, the cleanup function is invoked before the React component...
Read more >

github_iconTop Related Medium Post

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