The case against react/require-default-props
See original GitHub issueEver 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:
- Created 6 years ago
- Reactions:6
- Comments:11
Top GitHub Comments
I do not understand the argument regarding performance.
I do understand the argument:
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 oftitle
to be “string or null”.