React - setState in componentWillReceiveProps












33















Is this legal?



componentWillReceiveProps: function(nextProps) {
if (typeof nextProps.contact != 'undefined') {
this.setState({forename: nextProps.contact.forename});
this.setState({surname: nextProps.contact.surname});
this.setState({phone: nextProps.contact.phone});
this.setState({email: nextProps.contact.email});
}
}


Because I don't know how to fill my inputs and still be able that the user can edit the inputs. So I came up with this solution instead of trying to manipulate the this.props.



Any suggestions?










share|improve this question


















  • 3





    For anyone stepping on this in 2018, you might wanna use getDerivedStateFromProps()

    – Dane
    Apr 19 '18 at 4:57
















33















Is this legal?



componentWillReceiveProps: function(nextProps) {
if (typeof nextProps.contact != 'undefined') {
this.setState({forename: nextProps.contact.forename});
this.setState({surname: nextProps.contact.surname});
this.setState({phone: nextProps.contact.phone});
this.setState({email: nextProps.contact.email});
}
}


Because I don't know how to fill my inputs and still be able that the user can edit the inputs. So I came up with this solution instead of trying to manipulate the this.props.



Any suggestions?










share|improve this question


















  • 3





    For anyone stepping on this in 2018, you might wanna use getDerivedStateFromProps()

    – Dane
    Apr 19 '18 at 4:57














33












33








33


3






Is this legal?



componentWillReceiveProps: function(nextProps) {
if (typeof nextProps.contact != 'undefined') {
this.setState({forename: nextProps.contact.forename});
this.setState({surname: nextProps.contact.surname});
this.setState({phone: nextProps.contact.phone});
this.setState({email: nextProps.contact.email});
}
}


Because I don't know how to fill my inputs and still be able that the user can edit the inputs. So I came up with this solution instead of trying to manipulate the this.props.



Any suggestions?










share|improve this question














Is this legal?



componentWillReceiveProps: function(nextProps) {
if (typeof nextProps.contact != 'undefined') {
this.setState({forename: nextProps.contact.forename});
this.setState({surname: nextProps.contact.surname});
this.setState({phone: nextProps.contact.phone});
this.setState({email: nextProps.contact.email});
}
}


Because I don't know how to fill my inputs and still be able that the user can edit the inputs. So I came up with this solution instead of trying to manipulate the this.props.



Any suggestions?







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 6 '15 at 15:08









BeneBene

98611427




98611427








  • 3





    For anyone stepping on this in 2018, you might wanna use getDerivedStateFromProps()

    – Dane
    Apr 19 '18 at 4:57














  • 3





    For anyone stepping on this in 2018, you might wanna use getDerivedStateFromProps()

    – Dane
    Apr 19 '18 at 4:57








3




3





For anyone stepping on this in 2018, you might wanna use getDerivedStateFromProps()

– Dane
Apr 19 '18 at 4:57





For anyone stepping on this in 2018, you might wanna use getDerivedStateFromProps()

– Dane
Apr 19 '18 at 4:57












4 Answers
4






active

oldest

votes


















36














Your code is legal according to react documentation.



You also may consider to put this code inside getInitialState method as according to another react doc initializing from props is not an anti-pattern.



You also can replace several calls with one setState method call:



 this.setState({forename: nextProps.contact.forename,
surname: nextProps.contact.surname,
phone: nextProps.contact.phone,
email: nextProps.contact.email});





share|improve this answer































    1














    According to this react doc , changing states inside componentWillReceiveProps is not recommended. The doc explains it very clearly.



    And it also gives us alternative solutions for "fully controlled component" and "uncontrolled components", please read this react doc






    share|improve this answer

































      1














      As of React v16.6.3 this is considered UNSAFE and componentWillReceiveProps is marked for deprecation. The removal is planned to happen in version 17.




      Note:



      Using this lifecycle method often leads to bugs and inconsistencies, and for that reason it is going to be deprecated in the future.



      If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.



      For other use cases, follow the recommendations in this blog post about derived state.



      If you used componentWillReceiveProps for re-computing some data only when a prop changes, use a memoization helper instead.



      If you used componentWillReceiveProps to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.



      In very rare cases, you might want to use the getDerivedStateFromProps lifecycle as a last resort.




      In your case you should use componentDidUpdate instead.






      share|improve this answer































        0














        The only reason componentWillReceiveProps exists is to give the component an opportunity to setState. So yes, any state you set synchronously in it will be processed together with the new 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%2f31859391%2freact-setstate-in-componentwillreceiveprops%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          36














          Your code is legal according to react documentation.



          You also may consider to put this code inside getInitialState method as according to another react doc initializing from props is not an anti-pattern.



          You also can replace several calls with one setState method call:



           this.setState({forename: nextProps.contact.forename,
          surname: nextProps.contact.surname,
          phone: nextProps.contact.phone,
          email: nextProps.contact.email});





          share|improve this answer




























            36














            Your code is legal according to react documentation.



            You also may consider to put this code inside getInitialState method as according to another react doc initializing from props is not an anti-pattern.



            You also can replace several calls with one setState method call:



             this.setState({forename: nextProps.contact.forename,
            surname: nextProps.contact.surname,
            phone: nextProps.contact.phone,
            email: nextProps.contact.email});





            share|improve this answer


























              36












              36








              36







              Your code is legal according to react documentation.



              You also may consider to put this code inside getInitialState method as according to another react doc initializing from props is not an anti-pattern.



              You also can replace several calls with one setState method call:



               this.setState({forename: nextProps.contact.forename,
              surname: nextProps.contact.surname,
              phone: nextProps.contact.phone,
              email: nextProps.contact.email});





              share|improve this answer













              Your code is legal according to react documentation.



              You also may consider to put this code inside getInitialState method as according to another react doc initializing from props is not an anti-pattern.



              You also can replace several calls with one setState method call:



               this.setState({forename: nextProps.contact.forename,
              surname: nextProps.contact.surname,
              phone: nextProps.contact.phone,
              email: nextProps.contact.email});






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 6 '15 at 15:48









              Mikhail RomanovMikhail Romanov

              1,2441817




              1,2441817

























                  1














                  According to this react doc , changing states inside componentWillReceiveProps is not recommended. The doc explains it very clearly.



                  And it also gives us alternative solutions for "fully controlled component" and "uncontrolled components", please read this react doc






                  share|improve this answer






























                    1














                    According to this react doc , changing states inside componentWillReceiveProps is not recommended. The doc explains it very clearly.



                    And it also gives us alternative solutions for "fully controlled component" and "uncontrolled components", please read this react doc






                    share|improve this answer




























                      1












                      1








                      1







                      According to this react doc , changing states inside componentWillReceiveProps is not recommended. The doc explains it very clearly.



                      And it also gives us alternative solutions for "fully controlled component" and "uncontrolled components", please read this react doc






                      share|improve this answer















                      According to this react doc , changing states inside componentWillReceiveProps is not recommended. The doc explains it very clearly.



                      And it also gives us alternative solutions for "fully controlled component" and "uncontrolled components", please read this react doc







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 19 '18 at 15:22

























                      answered Jul 19 '18 at 15:16









                      HaimengHaimeng

                      113




                      113























                          1














                          As of React v16.6.3 this is considered UNSAFE and componentWillReceiveProps is marked for deprecation. The removal is planned to happen in version 17.




                          Note:



                          Using this lifecycle method often leads to bugs and inconsistencies, and for that reason it is going to be deprecated in the future.



                          If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.



                          For other use cases, follow the recommendations in this blog post about derived state.



                          If you used componentWillReceiveProps for re-computing some data only when a prop changes, use a memoization helper instead.



                          If you used componentWillReceiveProps to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.



                          In very rare cases, you might want to use the getDerivedStateFromProps lifecycle as a last resort.




                          In your case you should use componentDidUpdate instead.






                          share|improve this answer




























                            1














                            As of React v16.6.3 this is considered UNSAFE and componentWillReceiveProps is marked for deprecation. The removal is planned to happen in version 17.




                            Note:



                            Using this lifecycle method often leads to bugs and inconsistencies, and for that reason it is going to be deprecated in the future.



                            If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.



                            For other use cases, follow the recommendations in this blog post about derived state.



                            If you used componentWillReceiveProps for re-computing some data only when a prop changes, use a memoization helper instead.



                            If you used componentWillReceiveProps to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.



                            In very rare cases, you might want to use the getDerivedStateFromProps lifecycle as a last resort.




                            In your case you should use componentDidUpdate instead.






                            share|improve this answer


























                              1












                              1








                              1







                              As of React v16.6.3 this is considered UNSAFE and componentWillReceiveProps is marked for deprecation. The removal is planned to happen in version 17.




                              Note:



                              Using this lifecycle method often leads to bugs and inconsistencies, and for that reason it is going to be deprecated in the future.



                              If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.



                              For other use cases, follow the recommendations in this blog post about derived state.



                              If you used componentWillReceiveProps for re-computing some data only when a prop changes, use a memoization helper instead.



                              If you used componentWillReceiveProps to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.



                              In very rare cases, you might want to use the getDerivedStateFromProps lifecycle as a last resort.




                              In your case you should use componentDidUpdate instead.






                              share|improve this answer













                              As of React v16.6.3 this is considered UNSAFE and componentWillReceiveProps is marked for deprecation. The removal is planned to happen in version 17.




                              Note:



                              Using this lifecycle method often leads to bugs and inconsistencies, and for that reason it is going to be deprecated in the future.



                              If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.



                              For other use cases, follow the recommendations in this blog post about derived state.



                              If you used componentWillReceiveProps for re-computing some data only when a prop changes, use a memoization helper instead.



                              If you used componentWillReceiveProps to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.



                              In very rare cases, you might want to use the getDerivedStateFromProps lifecycle as a last resort.




                              In your case you should use componentDidUpdate instead.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 23 '18 at 8:36









                              gyosifovgyosifov

                              1,84121732




                              1,84121732























                                  0














                                  The only reason componentWillReceiveProps exists is to give the component an opportunity to setState. So yes, any state you set synchronously in it will be processed together with the new props.






                                  share|improve this answer




























                                    0














                                    The only reason componentWillReceiveProps exists is to give the component an opportunity to setState. So yes, any state you set synchronously in it will be processed together with the new props.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      The only reason componentWillReceiveProps exists is to give the component an opportunity to setState. So yes, any state you set synchronously in it will be processed together with the new props.






                                      share|improve this answer













                                      The only reason componentWillReceiveProps exists is to give the component an opportunity to setState. So yes, any state you set synchronously in it will be processed together with the new props.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 20 '18 at 10:49









                                      ShivamShivam

                                      1,1921332




                                      1,1921332






























                                          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%2f31859391%2freact-setstate-in-componentwillreceiveprops%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

                                          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                          Alcedinidae

                                          Can an atomic nucleus contain both particles and antiparticles? [duplicate]