Answer the question
In order to leave comments, you need to log in
Cloning part of state before update?
By focus on the input, I try to clone this.state.collection and write it to oldCollection ,
after changing collection, it changes after oldCollection
//исходное состояние
this.state = {
collection: {item:true}
oldCollection:null
};
onFocus() {//копирую по фокусу
this.setState({
oldCollection: this.state.collection
});
}
itemChange(event) {//меняю исходный объект
let collection = {item:false}
this.setState({
collection
});
console.log(this.state.oldCollection)//{item:false}
}
Answer the question
In order to leave comments, you need to log in
this.setState({
oldCollection: JSON.parse(JSON.stringify(collection))
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question