score:0

You don't have to do super(props). That is done only to access props inside the constructor. You can write:

constructor(){
 super()
 this.state={}
}

If you do not pass super(props):

 constructor(props){
  super()
  console.log('this.props', this.props); // => undefined
  console.log('props', props); // => whatever the props is
 }

Refer this stackoverflow answer

score:1

The constructor for a React component is called before it is mounted. When implementing the constructor for a React component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.

Read Here for more details.

score:1

If you're not going to use this.props in your constructor, you don't have to put it into super() like this:

constructor(){
   super();
   this.state = { randomState: "" };
   this.randomProperty = null;
}

But, in some cases, props passed from the parent component can be accessed and used inside constructor for initializing state (these props are not affected by prop change). By passing props to super, you can now use this.props inside constructor.

constructor(props){
   super(props);
   this.state = { randomVar: props.initialValue, propDependentState: this.props.someValue };
   this.someVar = props.defaultValue;
   this.anotherVar = this.props.openToChangesProp;
}

Take note that those props that are directly passed to this component are the only props accessible to the constructor. props that are mapped from state using redux connect is included in the props that can't be accessed here since the component hasn't mounted yet.


Related Query

More Query from same tag