problem description
the title is not described very clearly. Rearrange it here.
although you know that push
, splice
these operations will affect the original array. But will it still affect each other after state state storage?
suppose there is an array array
, and I have operated newArray: this.state.array
through setState
, and then I change newArray will also affect the value of array. Is there any way to solve it?
the environmental background of the problems and what methods you have tried
related codes
componentDidMount() {
let array = [1, 2, 3];
this.setState({
newArray: this.state.array
})
}
handleChange() {
let { newArray } = this.state;
newArray.push(4);
console.log(array);
}
what result do you expect? What is the error message actually seen?
the above result will output [1, 2, 3, 4]
. Is it really caused by push and splice changing the original array? Is there any way to solve it? Thank you