You need super(props);
only if you gonna use this.props
in the constructor. Otherwise you can use super();
If you use super();
in the constructor it is not a problem that outside of the constructor you will call this.props
.
You can read about it in the following link:
https://overreacted.io/why-do-we-write-super-props/
class Button extends React.Component {
constructor(props) {
super(); //we forgot to pass props
console.log(props); //{}
console.log(this.props); //undefined
}
// ...
}
It can be even more challenging if this happens in some method that’s called from the constructor. And that’s why I recommend always passing down super(props)
, even through it isn’t necessary.
class Button extends React.Component {
constructor(props) {
super(props); //we passed props
console.log(props); //{}
console.log(this.props); //{}
}
// ...
}