Display a warning if Component is called without props
See original GitHub issueDo you want to request a feature or report a bug?
This is a feature (enhancement).
What is the current behavior?
props
parameter isn’t validated in Component
and PureComponent
. Omitted props
is a common mistake that results in undefined this.props
in constructor. This may result in a problem:
class MyComponent extends Component {
constructor() {
super();
this.state = { a: 1, b: this.props.b }; // cannot read b property of undefined
}
}
A problem may be harder to determine if previously working code stops working when refactored:
class MyComponent extends Component {
constructor() {
super(); // no error
}
componentWillMount() {
// rewriting this to constructor code will result in situation above
this.setState({ a: 1, b: this.props.b });
}
}
What is the expected behavior?
Component
and PureComponent
validate props
parameter to be an object, or arguments > 0
at least and display a warning in development mode in case props
isn’t passed from child constructor.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.7.0-alpha.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Getting error message "li: 'key' is not a prop" - reactjs
Warning : li: 'key' is not a prop. Trying to access it will result in 'undefined' being returned. If you need to access...
Read more >How passing props to component works in React
Master how to pass props (properties) to components in React with this useful beginner's guide, including demo code.
Read more >Reacting to Prop Changes in a React Component
When building a component using React there is often a requirement to create a side effect when one of the component props changes....
Read more >Unknown Prop Warning
The unknown-prop warning will fire if you attempt to render a DOM element with a prop that is not recognized by React as...
Read more >Displaying Data with Props - From JavaScript to React
In the same way, you can pass pieces of information as properties to React components. These are called props . Similar to a...
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 Free
Top 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
This is a good idea, but since we made
super();
work if you don’t access this.props in the constructor, and using this.props in the constructor is relatively rare, I don’t think it is worth it to add this atsuper();
time.However we could perhaps have a warning when accessing this.props if we make it a getter?
Hi @bisubus, I agree with you but how can it helps to developers. You can also try static state or methods for the same.
Check out this article for more reference. https://michalzalecki.com/react-components-and-class-properties/