What is the difference between super() and super(props) in React using ES6 classes?

When you want to access this.props in constructor() then you should pass props to super() method.

Using super(props):

class MyComponent extends React.Component {
constructor(props) {
super(props);
console.log(this.props); // { name: 'John', ... }
}
}

Using super():

class MyComponent extends React.Component {
constructor(props) {
super();
console.log(this.props); // undefined
}
}

Outside constructor() both will display same value for this.props.


Made in India with ❤️ by Rajesh Kumar Yadav

Twitter