Why does react class component always need to call super(props) in its constructor?
In the tutorial published by reactjs.org, it was stated that "Class components should always call the base constructor with props
". In my own research, it seems that super(props)
can be replaced by super()
if this.props
is not used in the constructors, according to this StackOverflow answer.
Therefore, my question is, why should we always pass in props
to base constructor in reactjs? Is the advice sound? Why is it sound (or not sound) advice?
P.S. A screenshot is uploaded to this question just in case the original tutorial is updated while this question is being answered.
javascript reactjs
add a comment |
In the tutorial published by reactjs.org, it was stated that "Class components should always call the base constructor with props
". In my own research, it seems that super(props)
can be replaced by super()
if this.props
is not used in the constructors, according to this StackOverflow answer.
Therefore, my question is, why should we always pass in props
to base constructor in reactjs? Is the advice sound? Why is it sound (or not sound) advice?
P.S. A screenshot is uploaded to this question just in case the original tutorial is updated while this question is being answered.
javascript reactjs
React aside, you should always be calling the parent constructor with the arguments it expects. You cannot assume that not calling the parent constructor will be fine.
– Felix Kling
4 hours ago
Well, I think my question is on theprops
part, not thesuper()
part. However, I am very surprised to get a lot of knowledge from answers concerning thesuper()
part.
– Ezo T.
4 hours ago
add a comment |
In the tutorial published by reactjs.org, it was stated that "Class components should always call the base constructor with props
". In my own research, it seems that super(props)
can be replaced by super()
if this.props
is not used in the constructors, according to this StackOverflow answer.
Therefore, my question is, why should we always pass in props
to base constructor in reactjs? Is the advice sound? Why is it sound (or not sound) advice?
P.S. A screenshot is uploaded to this question just in case the original tutorial is updated while this question is being answered.
javascript reactjs
In the tutorial published by reactjs.org, it was stated that "Class components should always call the base constructor with props
". In my own research, it seems that super(props)
can be replaced by super()
if this.props
is not used in the constructors, according to this StackOverflow answer.
Therefore, my question is, why should we always pass in props
to base constructor in reactjs? Is the advice sound? Why is it sound (or not sound) advice?
P.S. A screenshot is uploaded to this question just in case the original tutorial is updated while this question is being answered.
javascript reactjs
javascript reactjs
asked 5 hours ago
Ezo T.Ezo T.
82110
82110
React aside, you should always be calling the parent constructor with the arguments it expects. You cannot assume that not calling the parent constructor will be fine.
– Felix Kling
4 hours ago
Well, I think my question is on theprops
part, not thesuper()
part. However, I am very surprised to get a lot of knowledge from answers concerning thesuper()
part.
– Ezo T.
4 hours ago
add a comment |
React aside, you should always be calling the parent constructor with the arguments it expects. You cannot assume that not calling the parent constructor will be fine.
– Felix Kling
4 hours ago
Well, I think my question is on theprops
part, not thesuper()
part. However, I am very surprised to get a lot of knowledge from answers concerning thesuper()
part.
– Ezo T.
4 hours ago
React aside, you should always be calling the parent constructor with the arguments it expects. You cannot assume that not calling the parent constructor will be fine.
– Felix Kling
4 hours ago
React aside, you should always be calling the parent constructor with the arguments it expects. You cannot assume that not calling the parent constructor will be fine.
– Felix Kling
4 hours ago
Well, I think my question is on the
props
part, not the super()
part. However, I am very surprised to get a lot of knowledge from answers concerning the super()
part.– Ezo T.
4 hours ago
Well, I think my question is on the
props
part, not the super()
part. However, I am very surprised to get a lot of knowledge from answers concerning the super()
part.– Ezo T.
4 hours ago
add a comment |
6 Answers
6
active
oldest
votes
Although it is suggested to pass props to
super
but it isn't
strictly necessary.
Passing it just helps in an odd situation where you might have a called a method in constructor and then at some point of time in future decided to use props in it. Now since props aren't available in the constructor since you haven't passed props to super
, it would result in an error. This kind of situation might be tricky to debug and hence its recommended to pass props always whenever you write a constructor so that it ensures this.props
is set even before the constructor exits.
It might also be worth mentioning what thesuper
constructor actually does - namely it setsthis.props
,this.context
, and some internal data structures.
– Birjolaxew
25 mins ago
add a comment |
This is because you are initializing the parent component React.Component
e.g. this.props
will be undefined. You can find detailed explanation for why we are using it in blog by Dan Abramov, react engineer
add a comment |
It is not necessary Is super necessary?
since class Clock extends React.Component, you can access all the properties and methods of React.Component.
By calling super, you are actually calling the parent element with props parameter
If you intend on using this.props inside the constructor, you have to call super(props)
add a comment |
In JavaScript, super refers to the parent class constructor.
Why do we call super? Can we not call it? If we have to call it, what happens if we don’t pass props? Are there any other arguments?
Read the article that is linked below to get the answers for the above questions.
Read the article from Dan Abramov (Works at React JS team)
add a comment |
In simple words,
You don't necessary have to pass props in constructor
or super
, You can directly use this.props
and it will still work.
You only need to pass props in constructor
and super
when you call a method inside constructor from which the value is from a prop.
add a comment |
In simple words, its used to call your parent class constructor which is very much required, when calling super(), it actually calls the parent class constructor and also in
ES class constructor must have super()
when its a subclass andsuper()
is required only when you have a constructor explicitly.
Super() not required
class TestApp extends React.component{
render(){
return <div>I don't need a super()</div>
}
}
Super() required
class TestApp extends React.component{
constructor(){
super(); // here super is required becuase it calls the parent class constructor //
}
render(){
return <div>I don't need a super()</div>
}
}
Using super()
you can also pass props super(props)
to the parent class, in this case, props
will be accessible across components via this.props
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54434041%2fwhy-does-react-class-component-always-need-to-call-superprops-in-its-construct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Although it is suggested to pass props to
super
but it isn't
strictly necessary.
Passing it just helps in an odd situation where you might have a called a method in constructor and then at some point of time in future decided to use props in it. Now since props aren't available in the constructor since you haven't passed props to super
, it would result in an error. This kind of situation might be tricky to debug and hence its recommended to pass props always whenever you write a constructor so that it ensures this.props
is set even before the constructor exits.
It might also be worth mentioning what thesuper
constructor actually does - namely it setsthis.props
,this.context
, and some internal data structures.
– Birjolaxew
25 mins ago
add a comment |
Although it is suggested to pass props to
super
but it isn't
strictly necessary.
Passing it just helps in an odd situation where you might have a called a method in constructor and then at some point of time in future decided to use props in it. Now since props aren't available in the constructor since you haven't passed props to super
, it would result in an error. This kind of situation might be tricky to debug and hence its recommended to pass props always whenever you write a constructor so that it ensures this.props
is set even before the constructor exits.
It might also be worth mentioning what thesuper
constructor actually does - namely it setsthis.props
,this.context
, and some internal data structures.
– Birjolaxew
25 mins ago
add a comment |
Although it is suggested to pass props to
super
but it isn't
strictly necessary.
Passing it just helps in an odd situation where you might have a called a method in constructor and then at some point of time in future decided to use props in it. Now since props aren't available in the constructor since you haven't passed props to super
, it would result in an error. This kind of situation might be tricky to debug and hence its recommended to pass props always whenever you write a constructor so that it ensures this.props
is set even before the constructor exits.
Although it is suggested to pass props to
super
but it isn't
strictly necessary.
Passing it just helps in an odd situation where you might have a called a method in constructor and then at some point of time in future decided to use props in it. Now since props aren't available in the constructor since you haven't passed props to super
, it would result in an error. This kind of situation might be tricky to debug and hence its recommended to pass props always whenever you write a constructor so that it ensures this.props
is set even before the constructor exits.
answered 4 hours ago
Shubham KhatriShubham Khatri
81.7k1599138
81.7k1599138
It might also be worth mentioning what thesuper
constructor actually does - namely it setsthis.props
,this.context
, and some internal data structures.
– Birjolaxew
25 mins ago
add a comment |
It might also be worth mentioning what thesuper
constructor actually does - namely it setsthis.props
,this.context
, and some internal data structures.
– Birjolaxew
25 mins ago
It might also be worth mentioning what the
super
constructor actually does - namely it sets this.props
, this.context
, and some internal data structures.– Birjolaxew
25 mins ago
It might also be worth mentioning what the
super
constructor actually does - namely it sets this.props
, this.context
, and some internal data structures.– Birjolaxew
25 mins ago
add a comment |
This is because you are initializing the parent component React.Component
e.g. this.props
will be undefined. You can find detailed explanation for why we are using it in blog by Dan Abramov, react engineer
add a comment |
This is because you are initializing the parent component React.Component
e.g. this.props
will be undefined. You can find detailed explanation for why we are using it in blog by Dan Abramov, react engineer
add a comment |
This is because you are initializing the parent component React.Component
e.g. this.props
will be undefined. You can find detailed explanation for why we are using it in blog by Dan Abramov, react engineer
This is because you are initializing the parent component React.Component
e.g. this.props
will be undefined. You can find detailed explanation for why we are using it in blog by Dan Abramov, react engineer
answered 4 hours ago
Umair FarooqUmair Farooq
80749
80749
add a comment |
add a comment |
It is not necessary Is super necessary?
since class Clock extends React.Component, you can access all the properties and methods of React.Component.
By calling super, you are actually calling the parent element with props parameter
If you intend on using this.props inside the constructor, you have to call super(props)
add a comment |
It is not necessary Is super necessary?
since class Clock extends React.Component, you can access all the properties and methods of React.Component.
By calling super, you are actually calling the parent element with props parameter
If you intend on using this.props inside the constructor, you have to call super(props)
add a comment |
It is not necessary Is super necessary?
since class Clock extends React.Component, you can access all the properties and methods of React.Component.
By calling super, you are actually calling the parent element with props parameter
If you intend on using this.props inside the constructor, you have to call super(props)
It is not necessary Is super necessary?
since class Clock extends React.Component, you can access all the properties and methods of React.Component.
By calling super, you are actually calling the parent element with props parameter
If you intend on using this.props inside the constructor, you have to call super(props)
answered 4 hours ago
Monica AchaMonica Acha
3249
3249
add a comment |
add a comment |
In JavaScript, super refers to the parent class constructor.
Why do we call super? Can we not call it? If we have to call it, what happens if we don’t pass props? Are there any other arguments?
Read the article that is linked below to get the answers for the above questions.
Read the article from Dan Abramov (Works at React JS team)
add a comment |
In JavaScript, super refers to the parent class constructor.
Why do we call super? Can we not call it? If we have to call it, what happens if we don’t pass props? Are there any other arguments?
Read the article that is linked below to get the answers for the above questions.
Read the article from Dan Abramov (Works at React JS team)
add a comment |
In JavaScript, super refers to the parent class constructor.
Why do we call super? Can we not call it? If we have to call it, what happens if we don’t pass props? Are there any other arguments?
Read the article that is linked below to get the answers for the above questions.
Read the article from Dan Abramov (Works at React JS team)
In JavaScript, super refers to the parent class constructor.
Why do we call super? Can we not call it? If we have to call it, what happens if we don’t pass props? Are there any other arguments?
Read the article that is linked below to get the answers for the above questions.
Read the article from Dan Abramov (Works at React JS team)
answered 4 hours ago
Raaj NadarRaaj Nadar
1,09521120
1,09521120
add a comment |
add a comment |
In simple words,
You don't necessary have to pass props in constructor
or super
, You can directly use this.props
and it will still work.
You only need to pass props in constructor
and super
when you call a method inside constructor from which the value is from a prop.
add a comment |
In simple words,
You don't necessary have to pass props in constructor
or super
, You can directly use this.props
and it will still work.
You only need to pass props in constructor
and super
when you call a method inside constructor from which the value is from a prop.
add a comment |
In simple words,
You don't necessary have to pass props in constructor
or super
, You can directly use this.props
and it will still work.
You only need to pass props in constructor
and super
when you call a method inside constructor from which the value is from a prop.
In simple words,
You don't necessary have to pass props in constructor
or super
, You can directly use this.props
and it will still work.
You only need to pass props in constructor
and super
when you call a method inside constructor from which the value is from a prop.
answered 3 hours ago
Thanveer ShahThanveer Shah
37910
37910
add a comment |
add a comment |
In simple words, its used to call your parent class constructor which is very much required, when calling super(), it actually calls the parent class constructor and also in
ES class constructor must have super()
when its a subclass andsuper()
is required only when you have a constructor explicitly.
Super() not required
class TestApp extends React.component{
render(){
return <div>I don't need a super()</div>
}
}
Super() required
class TestApp extends React.component{
constructor(){
super(); // here super is required becuase it calls the parent class constructor //
}
render(){
return <div>I don't need a super()</div>
}
}
Using super()
you can also pass props super(props)
to the parent class, in this case, props
will be accessible across components via this.props
.
add a comment |
In simple words, its used to call your parent class constructor which is very much required, when calling super(), it actually calls the parent class constructor and also in
ES class constructor must have super()
when its a subclass andsuper()
is required only when you have a constructor explicitly.
Super() not required
class TestApp extends React.component{
render(){
return <div>I don't need a super()</div>
}
}
Super() required
class TestApp extends React.component{
constructor(){
super(); // here super is required becuase it calls the parent class constructor //
}
render(){
return <div>I don't need a super()</div>
}
}
Using super()
you can also pass props super(props)
to the parent class, in this case, props
will be accessible across components via this.props
.
add a comment |
In simple words, its used to call your parent class constructor which is very much required, when calling super(), it actually calls the parent class constructor and also in
ES class constructor must have super()
when its a subclass andsuper()
is required only when you have a constructor explicitly.
Super() not required
class TestApp extends React.component{
render(){
return <div>I don't need a super()</div>
}
}
Super() required
class TestApp extends React.component{
constructor(){
super(); // here super is required becuase it calls the parent class constructor //
}
render(){
return <div>I don't need a super()</div>
}
}
Using super()
you can also pass props super(props)
to the parent class, in this case, props
will be accessible across components via this.props
.
In simple words, its used to call your parent class constructor which is very much required, when calling super(), it actually calls the parent class constructor and also in
ES class constructor must have super()
when its a subclass andsuper()
is required only when you have a constructor explicitly.
Super() not required
class TestApp extends React.component{
render(){
return <div>I don't need a super()</div>
}
}
Super() required
class TestApp extends React.component{
constructor(){
super(); // here super is required becuase it calls the parent class constructor //
}
render(){
return <div>I don't need a super()</div>
}
}
Using super()
you can also pass props super(props)
to the parent class, in this case, props
will be accessible across components via this.props
.
edited 3 hours ago
answered 4 hours ago
Praveen Rao Chavan.GPraveen Rao Chavan.G
1,306716
1,306716
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54434041%2fwhy-does-react-class-component-always-need-to-call-superprops-in-its-construct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
React aside, you should always be calling the parent constructor with the arguments it expects. You cannot assume that not calling the parent constructor will be fine.
– Felix Kling
4 hours ago
Well, I think my question is on the
props
part, not thesuper()
part. However, I am very surprised to get a lot of knowledge from answers concerning thesuper()
part.– Ezo T.
4 hours ago