React: this.var declared in constructor is not updated when new props is passed to component










0















I am unable to understand why a variable initialized in constructor is not updated when new props is passed to child component.



when new props is sent to the component, this.data remains as the old prop's values. (Note this is just an example without any state changes to demonstrate, i know that using states is an alternative method)



constructor(props) 
super(props)
this.data = this.props.receivedData


render()
console.log(this.props.receivedData) -> prints new props data
console.log(this.data) -> retains old props data
return (
<svg
....
</svg>
)










share|improve this question


























    0















    I am unable to understand why a variable initialized in constructor is not updated when new props is passed to child component.



    when new props is sent to the component, this.data remains as the old prop's values. (Note this is just an example without any state changes to demonstrate, i know that using states is an alternative method)



    constructor(props) 
    super(props)
    this.data = this.props.receivedData


    render()
    console.log(this.props.receivedData) -> prints new props data
    console.log(this.data) -> retains old props data
    return (
    <svg
    ....
    </svg>
    )










    share|improve this question
























      0












      0








      0








      I am unable to understand why a variable initialized in constructor is not updated when new props is passed to child component.



      when new props is sent to the component, this.data remains as the old prop's values. (Note this is just an example without any state changes to demonstrate, i know that using states is an alternative method)



      constructor(props) 
      super(props)
      this.data = this.props.receivedData


      render()
      console.log(this.props.receivedData) -> prints new props data
      console.log(this.data) -> retains old props data
      return (
      <svg
      ....
      </svg>
      )










      share|improve this question














      I am unable to understand why a variable initialized in constructor is not updated when new props is passed to child component.



      when new props is sent to the component, this.data remains as the old prop's values. (Note this is just an example without any state changes to demonstrate, i know that using states is an alternative method)



      constructor(props) 
      super(props)
      this.data = this.props.receivedData


      render()
      console.log(this.props.receivedData) -> prints new props data
      console.log(this.data) -> retains old props data
      return (
      <svg
      ....
      </svg>
      )







      javascript reactjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:00









      doyzdoyz

      259415




      259415






















          3 Answers
          3






          active

          oldest

          votes


















          0














          The constructor doesn't get called again when new props are passed. It used to be that you would hook into the componentWillReceiveProps lifecycle method. However, that is considered bad juju as of late and you should use the getDerivedStateFromProps method instead.



          See the official docs.



          This article does a decent job of explaining it.






          share|improve this answer






























            0














            A constructor is called only first time when the component is created, where you are assigning the value.
            Next time you will receive the props in ComponenantWillReceiveProps(nextProps) method.
            Try following code:



             constructor(props) 
            super(props)
            this.data = this.props.receivedData;

            ComponenantWillReceiveProps(nextProps)
            if(this.props.receivedData !== nextProps.receivedData)
            this.data = nextProps.receivedData;//new data will be updated here


            render()
            console.log(this.props.receivedData)
            console.log(this.data)
            return (
            <svg
            </svg>
            )



            you can use state to update the value and render the view when new data is received.



            Ex:-



            constructor(props) 
            super(props)
            this.data = this.props.receivedData;
            this.state =
            data : this.props.receivedData
            ;


            ComponenantWillReceiveProps(nextProps)
            if(this.props.receivedData !== nextProps.receivedData)
            this.setState(data:nextProps.receivedData);//new data will be updated here


            render()
            console.log(this.props.receivedData)
            console.log(this.data)
            return (
            <div>this.state.data</div>
            )






            share|improve this answer
































              0














              Just to expand a little on the other answers. Depending on what you are trying to do, there are different ways of going about this. As the other answers state, the constructor will only run one time on the construction of the component.



              Now if you want to take the incoming props and store them as state, and then have that state updated as a result of new props then getDerivedStateFromProps might be what you need. If this is what you want however, its important to know why you want to do this in the first place because you may not need to. One common reason which causes people to opt for this pattern is to memoize props, but this can and should be achieved using a memoizing library. You can learn more about this here.



              The other reason why you might opt for this, is to simply have your component update as a result of props changing. Now you hinted to this in your question, but for the sake of clarity, all you would need to do is use the incoming props, and when they change the component would update as a result. In other words, in order to simply render the props, there is no reason to store them in state or instance variable.






              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%2f53306257%2freact-this-var-declared-in-constructor-is-not-updated-when-new-props-is-passed%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                The constructor doesn't get called again when new props are passed. It used to be that you would hook into the componentWillReceiveProps lifecycle method. However, that is considered bad juju as of late and you should use the getDerivedStateFromProps method instead.



                See the official docs.



                This article does a decent job of explaining it.






                share|improve this answer



























                  0














                  The constructor doesn't get called again when new props are passed. It used to be that you would hook into the componentWillReceiveProps lifecycle method. However, that is considered bad juju as of late and you should use the getDerivedStateFromProps method instead.



                  See the official docs.



                  This article does a decent job of explaining it.






                  share|improve this answer

























                    0












                    0








                    0







                    The constructor doesn't get called again when new props are passed. It used to be that you would hook into the componentWillReceiveProps lifecycle method. However, that is considered bad juju as of late and you should use the getDerivedStateFromProps method instead.



                    See the official docs.



                    This article does a decent job of explaining it.






                    share|improve this answer













                    The constructor doesn't get called again when new props are passed. It used to be that you would hook into the componentWillReceiveProps lifecycle method. However, that is considered bad juju as of late and you should use the getDerivedStateFromProps method instead.



                    See the official docs.



                    This article does a decent job of explaining it.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 18:06









                    colefnercolefner

                    1,29311311




                    1,29311311























                        0














                        A constructor is called only first time when the component is created, where you are assigning the value.
                        Next time you will receive the props in ComponenantWillReceiveProps(nextProps) method.
                        Try following code:



                         constructor(props) 
                        super(props)
                        this.data = this.props.receivedData;

                        ComponenantWillReceiveProps(nextProps)
                        if(this.props.receivedData !== nextProps.receivedData)
                        this.data = nextProps.receivedData;//new data will be updated here


                        render()
                        console.log(this.props.receivedData)
                        console.log(this.data)
                        return (
                        <svg
                        </svg>
                        )



                        you can use state to update the value and render the view when new data is received.



                        Ex:-



                        constructor(props) 
                        super(props)
                        this.data = this.props.receivedData;
                        this.state =
                        data : this.props.receivedData
                        ;


                        ComponenantWillReceiveProps(nextProps)
                        if(this.props.receivedData !== nextProps.receivedData)
                        this.setState(data:nextProps.receivedData);//new data will be updated here


                        render()
                        console.log(this.props.receivedData)
                        console.log(this.data)
                        return (
                        <div>this.state.data</div>
                        )






                        share|improve this answer





























                          0














                          A constructor is called only first time when the component is created, where you are assigning the value.
                          Next time you will receive the props in ComponenantWillReceiveProps(nextProps) method.
                          Try following code:



                           constructor(props) 
                          super(props)
                          this.data = this.props.receivedData;

                          ComponenantWillReceiveProps(nextProps)
                          if(this.props.receivedData !== nextProps.receivedData)
                          this.data = nextProps.receivedData;//new data will be updated here


                          render()
                          console.log(this.props.receivedData)
                          console.log(this.data)
                          return (
                          <svg
                          </svg>
                          )



                          you can use state to update the value and render the view when new data is received.



                          Ex:-



                          constructor(props) 
                          super(props)
                          this.data = this.props.receivedData;
                          this.state =
                          data : this.props.receivedData
                          ;


                          ComponenantWillReceiveProps(nextProps)
                          if(this.props.receivedData !== nextProps.receivedData)
                          this.setState(data:nextProps.receivedData);//new data will be updated here


                          render()
                          console.log(this.props.receivedData)
                          console.log(this.data)
                          return (
                          <div>this.state.data</div>
                          )






                          share|improve this answer



























                            0












                            0








                            0







                            A constructor is called only first time when the component is created, where you are assigning the value.
                            Next time you will receive the props in ComponenantWillReceiveProps(nextProps) method.
                            Try following code:



                             constructor(props) 
                            super(props)
                            this.data = this.props.receivedData;

                            ComponenantWillReceiveProps(nextProps)
                            if(this.props.receivedData !== nextProps.receivedData)
                            this.data = nextProps.receivedData;//new data will be updated here


                            render()
                            console.log(this.props.receivedData)
                            console.log(this.data)
                            return (
                            <svg
                            </svg>
                            )



                            you can use state to update the value and render the view when new data is received.



                            Ex:-



                            constructor(props) 
                            super(props)
                            this.data = this.props.receivedData;
                            this.state =
                            data : this.props.receivedData
                            ;


                            ComponenantWillReceiveProps(nextProps)
                            if(this.props.receivedData !== nextProps.receivedData)
                            this.setState(data:nextProps.receivedData);//new data will be updated here


                            render()
                            console.log(this.props.receivedData)
                            console.log(this.data)
                            return (
                            <div>this.state.data</div>
                            )






                            share|improve this answer















                            A constructor is called only first time when the component is created, where you are assigning the value.
                            Next time you will receive the props in ComponenantWillReceiveProps(nextProps) method.
                            Try following code:



                             constructor(props) 
                            super(props)
                            this.data = this.props.receivedData;

                            ComponenantWillReceiveProps(nextProps)
                            if(this.props.receivedData !== nextProps.receivedData)
                            this.data = nextProps.receivedData;//new data will be updated here


                            render()
                            console.log(this.props.receivedData)
                            console.log(this.data)
                            return (
                            <svg
                            </svg>
                            )



                            you can use state to update the value and render the view when new data is received.



                            Ex:-



                            constructor(props) 
                            super(props)
                            this.data = this.props.receivedData;
                            this.state =
                            data : this.props.receivedData
                            ;


                            ComponenantWillReceiveProps(nextProps)
                            if(this.props.receivedData !== nextProps.receivedData)
                            this.setState(data:nextProps.receivedData);//new data will be updated here


                            render()
                            console.log(this.props.receivedData)
                            console.log(this.data)
                            return (
                            <div>this.state.data</div>
                            )







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 14 '18 at 18:19

























                            answered Nov 14 '18 at 18:14









                            ShubhamShubham

                            190127




                            190127





















                                0














                                Just to expand a little on the other answers. Depending on what you are trying to do, there are different ways of going about this. As the other answers state, the constructor will only run one time on the construction of the component.



                                Now if you want to take the incoming props and store them as state, and then have that state updated as a result of new props then getDerivedStateFromProps might be what you need. If this is what you want however, its important to know why you want to do this in the first place because you may not need to. One common reason which causes people to opt for this pattern is to memoize props, but this can and should be achieved using a memoizing library. You can learn more about this here.



                                The other reason why you might opt for this, is to simply have your component update as a result of props changing. Now you hinted to this in your question, but for the sake of clarity, all you would need to do is use the incoming props, and when they change the component would update as a result. In other words, in order to simply render the props, there is no reason to store them in state or instance variable.






                                share|improve this answer



























                                  0














                                  Just to expand a little on the other answers. Depending on what you are trying to do, there are different ways of going about this. As the other answers state, the constructor will only run one time on the construction of the component.



                                  Now if you want to take the incoming props and store them as state, and then have that state updated as a result of new props then getDerivedStateFromProps might be what you need. If this is what you want however, its important to know why you want to do this in the first place because you may not need to. One common reason which causes people to opt for this pattern is to memoize props, but this can and should be achieved using a memoizing library. You can learn more about this here.



                                  The other reason why you might opt for this, is to simply have your component update as a result of props changing. Now you hinted to this in your question, but for the sake of clarity, all you would need to do is use the incoming props, and when they change the component would update as a result. In other words, in order to simply render the props, there is no reason to store them in state or instance variable.






                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    Just to expand a little on the other answers. Depending on what you are trying to do, there are different ways of going about this. As the other answers state, the constructor will only run one time on the construction of the component.



                                    Now if you want to take the incoming props and store them as state, and then have that state updated as a result of new props then getDerivedStateFromProps might be what you need. If this is what you want however, its important to know why you want to do this in the first place because you may not need to. One common reason which causes people to opt for this pattern is to memoize props, but this can and should be achieved using a memoizing library. You can learn more about this here.



                                    The other reason why you might opt for this, is to simply have your component update as a result of props changing. Now you hinted to this in your question, but for the sake of clarity, all you would need to do is use the incoming props, and when they change the component would update as a result. In other words, in order to simply render the props, there is no reason to store them in state or instance variable.






                                    share|improve this answer













                                    Just to expand a little on the other answers. Depending on what you are trying to do, there are different ways of going about this. As the other answers state, the constructor will only run one time on the construction of the component.



                                    Now if you want to take the incoming props and store them as state, and then have that state updated as a result of new props then getDerivedStateFromProps might be what you need. If this is what you want however, its important to know why you want to do this in the first place because you may not need to. One common reason which causes people to opt for this pattern is to memoize props, but this can and should be achieved using a memoizing library. You can learn more about this here.



                                    The other reason why you might opt for this, is to simply have your component update as a result of props changing. Now you hinted to this in your question, but for the sake of clarity, all you would need to do is use the incoming props, and when they change the component would update as a result. In other words, in order to simply render the props, there is no reason to store them in state or instance variable.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 14 '18 at 18:22









                                    Chaim FriedmanChaim Friedman

                                    2,787832




                                    2,787832



























                                        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%2f53306257%2freact-this-var-declared-in-constructor-is-not-updated-when-new-props-is-passed%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







                                        這個網誌中的熱門文章

                                        How to read a connectionString WITH PROVIDER in .NET Core?

                                        In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                        Museum of Modern and Contemporary Art of Trento and Rovereto