0 votes
in ReactJS by

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

1 Answer

0 votes
by

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.

Related questions

0 votes
asked Jul 2, 2019 in ReactJS by Venkatshastri
0 votes
asked Mar 2, 2020 in ECMAScript by rajeshsharma
...