how to get a value of the state and manipulate it without changing the state itself in react?











up vote
0
down vote

favorite












I'm developing an app with react native. I have an array of object in the state (lettersPosition) and I want to sort it temporally in a variable within a function (but I don't want the state itself to be sorted) :



verifyWord = () => {
const array = this.state.lettersPosition;
array.sort(function (a, b) {
return a.x - b.x;
});
var word = "";
array.map(function (char) {
word += char.letter
})
}


I tested it and it appears that my state itself was updated after the sorting (even if I called the sort function on the temp array).



It is like if the 'array' variable contains the whole reference to the state and not only its value. And if I modify that variable, it modifies the state too.



Is it a normal behaviour in react ?



How can I just get the value of the state and manipulate it without changing the state itself ?



Thanks










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm developing an app with react native. I have an array of object in the state (lettersPosition) and I want to sort it temporally in a variable within a function (but I don't want the state itself to be sorted) :



    verifyWord = () => {
    const array = this.state.lettersPosition;
    array.sort(function (a, b) {
    return a.x - b.x;
    });
    var word = "";
    array.map(function (char) {
    word += char.letter
    })
    }


    I tested it and it appears that my state itself was updated after the sorting (even if I called the sort function on the temp array).



    It is like if the 'array' variable contains the whole reference to the state and not only its value. And if I modify that variable, it modifies the state too.



    Is it a normal behaviour in react ?



    How can I just get the value of the state and manipulate it without changing the state itself ?



    Thanks










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm developing an app with react native. I have an array of object in the state (lettersPosition) and I want to sort it temporally in a variable within a function (but I don't want the state itself to be sorted) :



      verifyWord = () => {
      const array = this.state.lettersPosition;
      array.sort(function (a, b) {
      return a.x - b.x;
      });
      var word = "";
      array.map(function (char) {
      word += char.letter
      })
      }


      I tested it and it appears that my state itself was updated after the sorting (even if I called the sort function on the temp array).



      It is like if the 'array' variable contains the whole reference to the state and not only its value. And if I modify that variable, it modifies the state too.



      Is it a normal behaviour in react ?



      How can I just get the value of the state and manipulate it without changing the state itself ?



      Thanks










      share|improve this question













      I'm developing an app with react native. I have an array of object in the state (lettersPosition) and I want to sort it temporally in a variable within a function (but I don't want the state itself to be sorted) :



      verifyWord = () => {
      const array = this.state.lettersPosition;
      array.sort(function (a, b) {
      return a.x - b.x;
      });
      var word = "";
      array.map(function (char) {
      word += char.letter
      })
      }


      I tested it and it appears that my state itself was updated after the sorting (even if I called the sort function on the temp array).



      It is like if the 'array' variable contains the whole reference to the state and not only its value. And if I modify that variable, it modifies the state too.



      Is it a normal behaviour in react ?



      How can I just get the value of the state and manipulate it without changing the state itself ?



      Thanks







      reactjs react-native






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 13:18









      kikdu

      31




      31
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Use Array.from(). It creates a shallow copy.



          const sortedArray = Array.from(this.state.lettersPosition);
          ...etc





          share|improve this answer





















          • thanks it works
            – kikdu
            Nov 11 at 19:11


















          up vote
          0
          down vote













          Try using var newArray = oldArray.slice(); rather than const array = this.state.lettersPosition; In your code you're not creating a copy of the array, you're just assigning it to another variable.



          See Copy array by value for more info on var newArray = oldArray.slice();.






          share|improve this answer





















          • thanks it works
            – kikdu
            Nov 11 at 19:10


















          up vote
          0
          down vote













          You can use the spread operator too.



          var a = [1, 2, 3, 4, 5]
          var b = [...a]





          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',
            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%2f53249130%2fhow-to-get-a-value-of-the-state-and-manipulate-it-without-changing-the-state-its%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








            up vote
            0
            down vote



            accepted










            Use Array.from(). It creates a shallow copy.



            const sortedArray = Array.from(this.state.lettersPosition);
            ...etc





            share|improve this answer





















            • thanks it works
              – kikdu
              Nov 11 at 19:11















            up vote
            0
            down vote



            accepted










            Use Array.from(). It creates a shallow copy.



            const sortedArray = Array.from(this.state.lettersPosition);
            ...etc





            share|improve this answer





















            • thanks it works
              – kikdu
              Nov 11 at 19:11













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            Use Array.from(). It creates a shallow copy.



            const sortedArray = Array.from(this.state.lettersPosition);
            ...etc





            share|improve this answer












            Use Array.from(). It creates a shallow copy.



            const sortedArray = Array.from(this.state.lettersPosition);
            ...etc






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 17:07









            matt carlotta

            1,98749




            1,98749












            • thanks it works
              – kikdu
              Nov 11 at 19:11


















            • thanks it works
              – kikdu
              Nov 11 at 19:11
















            thanks it works
            – kikdu
            Nov 11 at 19:11




            thanks it works
            – kikdu
            Nov 11 at 19:11












            up vote
            0
            down vote













            Try using var newArray = oldArray.slice(); rather than const array = this.state.lettersPosition; In your code you're not creating a copy of the array, you're just assigning it to another variable.



            See Copy array by value for more info on var newArray = oldArray.slice();.






            share|improve this answer





















            • thanks it works
              – kikdu
              Nov 11 at 19:10















            up vote
            0
            down vote













            Try using var newArray = oldArray.slice(); rather than const array = this.state.lettersPosition; In your code you're not creating a copy of the array, you're just assigning it to another variable.



            See Copy array by value for more info on var newArray = oldArray.slice();.






            share|improve this answer





















            • thanks it works
              – kikdu
              Nov 11 at 19:10













            up vote
            0
            down vote










            up vote
            0
            down vote









            Try using var newArray = oldArray.slice(); rather than const array = this.state.lettersPosition; In your code you're not creating a copy of the array, you're just assigning it to another variable.



            See Copy array by value for more info on var newArray = oldArray.slice();.






            share|improve this answer












            Try using var newArray = oldArray.slice(); rather than const array = this.state.lettersPosition; In your code you're not creating a copy of the array, you're just assigning it to another variable.



            See Copy array by value for more info on var newArray = oldArray.slice();.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 13:22









            Tonis F. Piip

            11919




            11919












            • thanks it works
              – kikdu
              Nov 11 at 19:10


















            • thanks it works
              – kikdu
              Nov 11 at 19:10
















            thanks it works
            – kikdu
            Nov 11 at 19:10




            thanks it works
            – kikdu
            Nov 11 at 19:10










            up vote
            0
            down vote













            You can use the spread operator too.



            var a = [1, 2, 3, 4, 5]
            var b = [...a]





            share|improve this answer

























              up vote
              0
              down vote













              You can use the spread operator too.



              var a = [1, 2, 3, 4, 5]
              var b = [...a]





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You can use the spread operator too.



                var a = [1, 2, 3, 4, 5]
                var b = [...a]





                share|improve this answer












                You can use the spread operator too.



                var a = [1, 2, 3, 4, 5]
                var b = [...a]






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 17:11









                sridhar reddy

                41727




                41727






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53249130%2fhow-to-get-a-value-of-the-state-and-manipulate-it-without-changing-the-state-its%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

                    Full-time equivalent

                    Bicuculline

                    さくらももこ