我有我正在 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
据我所理解
(它不是另一个问题的重复,我不是在寻求一种有效的方法)