in React JS by
Q:

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.

Click here to read more about React JS
Click here to read more about Insurance

Related questions

0 votes
asked Mar 30, 2020 in React JS by amita rallin
0 votes
asked Jun 19, 2020 in React JS by JackTerrance
0 votes
asked Mar 1, 2020 in React JS by RShastri
0 votes
asked Mar 1, 2020 in React JS by RShastri
0 votes
0 votes
asked Jan 30 in React JS by rajeshsharma
...