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.

The case against react/require-default-props

See original GitHub issue

Ever since my project uses Airbnb’s eslint preset I’ve been dutifully complying with the react/require-default-props rule and adding defaultProps to my components even when those default properties were nothing more than '', null or 0 - just to satisfy the linter. No actual source code got changed as a result of this rule, as I just check for any falsy value and not a specific one.

One day, while chasing a bug, I encountered the source code of React.createElement() and witnessed the consequences of my obedience: even though defaultProps has no practical benefit to any of the code in the components, I was forcing React to apply those defaultProps in a loop every time they were created. The sole purpose of a frequently recommended optimization is to remove one loop from React.createElement() and here I was, adding one pointlessly. At once, I overrode Airbnb’s otherwise formidable eslint preset to stop forcing myself to waste V8’s precious time.

I propose that react/require-default-props and related textual recommendation be struck from your source and documentation.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:11

github_iconTop GitHub Comments

12reactions
stefek99commented, Oct 1, 2018

I do not understand the argument regarding performance.

I do understand the argument:

those default properties were nothing more than '', null or 0 - just to satisfy the linter.

3reactions
ljharbcommented, Mar 2, 2018

In this case, where there’s no use case for having a title as an empty string, I think what you have is fine.

However, if you wanted that to be possible, then I’d do null as the defaultProp. That way it’s at least explicit that you’ve overloaded the type of title to be “string or null”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A complete guide to React default props - LogRocket Blog
Cover three ways to implement default props in React and provide default values for props that are not required by the component.
Read more >
[TYPESCRIPT]: react/require-default-props does not work ...
reactjs - [TYPESCRIPT]: react/require-default-props does not work correctly with my . eslintrc configuration - Stack Overflow. Stack Overflow ...
Read more >
Stop using defaultProps (React 18.3 preview) - YouTube
Become a TypeScript Wizard with Matt's upcoming TypeScript Course:https://www.totaltypescript.com/Follow Matt on ...
Read more >
ReactJS defaultProps - GeeksforGeeks
The defaultProps is a React component property that allows you to set default values for the props argument. If the prop property is...
Read more >
React Pro Tip #2 — How to Type `this.props` to Include ...
It may be you can't make the props required, or feel it would be unnecessarily complicated to do so (for example, if many...
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