【发布时间】:2019-09-23 00:53:10
【问题描述】:
我有这段代码在 jsfiddle 上测试
onVote = (dir, index) => {
console.log(this.state)
const products = [...this.state.products]
products[index].votes = dir ? products[index].votes + 1 : products[index].votes - 1
console.log(this.state.products[index].votes)
// this.setState({products})
};
https://jsfiddle.net/hkL3wug7/2/
但是,即使我没有设置状态,控制台日志显示每次单击加号和减号时状态都会发生变化。
const newState = [...state] // clone the array
newState[action.index].done = true
return newState
据我所知
(这不是另一个问题的重复,我不是在要求一种有效的方法)
【问题讨论】:
-
这是列表结构的浅拷贝。这不会复制它包含的元素。您需要以某种方式复制它们。