When is it important to pass props
to super()
, and why?
class MyComponent extends React.Component {
constructor(props) {
super(); // or super(props) ?
}
}
When is it important to pass props
to super()
, and why?
class MyComponent extends React.Component {
constructor(props) {
super(); // or super(props) ?
}
}
Here we won't get this in the constructor so it will return undefined, but we will be able to fetch this outside the constructor function
If we are using super(), then we can fetch the "this" variable inside the constructor as well
So when we are using super(); we will be able to fetch this but this.props will be undefined in the constructor. But other than constructor, this.props will not return undefined.
If we use super(props), then we can use this.props value inside the constructor as well
Sophie Alpert's Answer
super()
is used to call the parent constructor.super(props)
would passprops
to the parent constructor.From your example,
super(props)
would call theReact.Component
constructor passing inprops
as the argument.More information on
super
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/superFor react version 16.6.3, we use super(props) to initialize state element name : this.props.name
There is only one reason when one needs to pass
props
tosuper()
:When you want to access
this.props
in constructor.Passing:
Not passing:
Note that passing or not passing
props
tosuper
has no effect on later uses ofthis.props
outsideconstructor
. That isrender
,shouldComponentUpdate
, or event handlers always have access to it.This is explicitly said in one Sophie Alpert's answer to a similar question.
The documentation—State and Lifecycle, Adding Local State to a Class, point 2—recommends:
However, no reason is provided. We can speculate it is either because of subclassing or for future compatibility.
(Thanks @MattBrowne for the link)
Here is the fiddle I've made: https://jsfiddle.net/beshanoe/zpxbLw4j/1/. It shows that props are assigned not in the constructor by default. As I understand they are assinged in the method
React.createElement
. Hencesuper(props)
should be called only when the superclass's constructor manually assingsprops
tothis.props
. If you just extend theReact.Component
callingsuper(props)
will do nothing with props. Maybe It will be changed in the next versions of React.When implementing the
constructor()
function inside a React component,super()
is a requirement. Keep in mind that yourMyComponent
component is extending or borrowing functionality from theReact.Component
base class.This base class has a
constructor()
function of its own that has some code inside of it, to setup our React component for us.When we define a
constructor()
function inside ourMyComponent
class, we are essentially, overriding or replacing theconstructor()
function that is inside theReact.Component
class, but we still need to ensure that all the setup code inside of thisconstructor()
function still gets called.So to ensure that the
React.Component
’sconstructor()
function gets called, we callsuper(props)
.super(props)
is a reference to the parentsconstructor()
function, that’s all it is.We have to add
super(props)
every single time we define aconstructor()
function inside a class-based component.If we don’t we will see an error saying that we have to call
super(props)
.The entire reason for defining this
constructor()
funciton is to initialize our state object.So in order to initialize our state object, underneath the super call I am going to write:
So we have defined our
constructor()
method, initialized our state object by creating a JavaScript object, assigning a property or key/value pair to it, assigning the result of that tothis.state
. Now of course this is just an example here so I have not really assigned a key/value pair to the state object, its just an empty object.