React: Enforce defaultProps for props that are not required
See original GitHub issueSummary
Please consider requiring defaultProps definition for all props that aren’t required.
Specifically, via react/require-default-props
eslint rule.
Justification
Without defaultProps
, a prop is set to undefined
. Sometimes this isn’t a problem, but sometimes it is, ~as in #6076~ (actually not exactly relevant in this case)
Additional examples of why defaults for non-required props are important can be found in the react/require-default-props
docs.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top 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 >Typechecking With PropTypes - React
The defaultProps will be used to ensure that this.props.name will have a value if it was not specified by the parent component. The...
Read more >propType "name" is not required, but has no corresponding ...
First is to use defaultProps directly, which is deprecated for function components. In your case it would look like:
Read more >React: Everything about Default Props | by Chidume Nnamdi
The Solution — defaultProps ... To solve this issue, we can use the logical operator || to set a fallback value, so when...
Read more >React with TypeScript: Optional Props with Default Values
But what if we want default values for our optional props in the case when they are not specified? ... We can then...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Going to close this out since it’s been a while, miss you at IBM @jendowns! Hope you’re having fun at your current gig 🥳
@jendowns it seems like that would have
''
as the default forclassName
, what would you recommend as a default forchildren
?