Why does react class component always need to call super(props) in its constructor?












7















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.



reactjs tutorial










share|improve this question























  • 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


















7















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.



reactjs tutorial










share|improve this question























  • 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
















7












7








7








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.



reactjs tutorial










share|improve this question














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.



reactjs tutorial







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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





















  • 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



















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














6 Answers
6






active

oldest

votes


















3















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.






share|improve this answer
























  • 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



















0














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






share|improve this answer































    0














    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)






    share|improve this answer































      0














      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)






      share|improve this answer































        0














        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.






        share|improve this answer































          0














          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.






          share|improve this answer

























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            3















            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.






            share|improve this answer
























            • 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
















            3















            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.






            share|improve this answer
























            • 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














            3












            3








            3








            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.






            share|improve this answer














            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 4 hours ago









            Shubham KhatriShubham Khatri

            81.7k1599138




            81.7k1599138













            • 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

















            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













            0














            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






            share|improve this answer




























              0














              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






              share|improve this answer


























                0












                0








                0







                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 4 hours ago









                Umair FarooqUmair Farooq

                80749




                80749























                    0














                    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)






                    share|improve this answer




























                      0














                      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)






                      share|improve this answer


























                        0












                        0








                        0







                        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)






                        share|improve this answer













                        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)







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 4 hours ago









                        Monica AchaMonica Acha

                        3249




                        3249























                            0














                            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)






                            share|improve this answer




























                              0














                              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)






                              share|improve this answer


























                                0












                                0








                                0







                                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)






                                share|improve this answer













                                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)







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 4 hours ago









                                Raaj NadarRaaj Nadar

                                1,09521120




                                1,09521120























                                    0














                                    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.






                                    share|improve this answer




























                                      0














                                      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.






                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        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.






                                        share|improve this answer













                                        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.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 3 hours ago









                                        Thanveer ShahThanveer Shah

                                        37910




                                        37910























                                            0














                                            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.






                                            share|improve this answer






























                                              0














                                              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.






                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                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.






                                                share|improve this answer















                                                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.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited 3 hours ago

























                                                answered 4 hours ago









                                                Praveen Rao Chavan.GPraveen Rao Chavan.G

                                                1,306716




                                                1,306716






























                                                    draft saved

                                                    draft discarded




















































                                                    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.




                                                    draft saved


                                                    draft discarded














                                                    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





















































                                                    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







                                                    Popular posts from this blog

                                                    GameSpot

                                                    connect to host localhost port 22: Connection refused

                                                    Getting a Wifi WPA2 wifi connection