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
reactjs react-native
add a comment |
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
reactjs react-native
add a comment |
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
reactjs react-native
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
reactjs react-native
asked Nov 11 at 13:18
kikdu
31
31
add a comment |
add a comment |
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
thanks it works
– kikdu
Nov 11 at 19:11
add a comment |
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();
.
thanks it works
– kikdu
Nov 11 at 19:10
add a comment |
up vote
0
down vote
You can use the spread operator too.
var a = [1, 2, 3, 4, 5]
var b = [...a]
add a comment |
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
thanks it works
– kikdu
Nov 11 at 19:11
add a comment |
up vote
0
down vote
accepted
Use Array.from(). It creates a shallow copy.
const sortedArray = Array.from(this.state.lettersPosition);
...etc
thanks it works
– kikdu
Nov 11 at 19:11
add a comment |
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
Use Array.from(). It creates a shallow copy.
const sortedArray = Array.from(this.state.lettersPosition);
...etc
answered Nov 11 at 17:07
matt carlotta
1,98749
1,98749
thanks it works
– kikdu
Nov 11 at 19:11
add a comment |
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
add a comment |
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();
.
thanks it works
– kikdu
Nov 11 at 19:10
add a comment |
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();
.
thanks it works
– kikdu
Nov 11 at 19:10
add a comment |
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();
.
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();
.
answered Nov 11 at 13:22
Tonis F. Piip
11919
11919
thanks it works
– kikdu
Nov 11 at 19:10
add a comment |
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
add a comment |
up vote
0
down vote
You can use the spread operator too.
var a = [1, 2, 3, 4, 5]
var b = [...a]
add a comment |
up vote
0
down vote
You can use the spread operator too.
var a = [1, 2, 3, 4, 5]
var b = [...a]
add a comment |
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]
You can use the spread operator too.
var a = [1, 2, 3, 4, 5]
var b = [...a]
answered Nov 11 at 17:11
sridhar reddy
41727
41727
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown