Member-only story
React | What is the difference between super()
and super(props)
in React using ES6 classes? |
In this React Series, I will be sharing interview questions on React.
Don’t forget to follow me for more such articles.
Let’s get started,
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
}
}
Don’t forget to follow me for more such articles.
Summary
Keep learning, keep growing!
Let’s connect on LinkedIn!. Please read more for more data structure javascript question.