【发布时间】:2020-08-14 23:06:14
【问题描述】:
我想在我的对象中将 active 更改为 true,在控制台日志中我有状态的更新版本但是我无法更新状态本身,我觉得我这样做是错误的。任何帮助都会非常感谢你,肯定在某个地方有类似的帖子,但我什么都没找到。
const [components, setComponents] = useState([
{
compName: "Weather",
path: 'weather',
active: false
},
{
compName: "Tasks",
path: 'tasks',
active: false
},
])
function onActivateHandler(){
setComponents((prevComponents) => {
const copy = [...prevComponents]
let newState = {...copy[1]}
newState.active = true
console.log(newState)
return {...prevComponents}
})
}
【问题讨论】:
标签: javascript reactjs react-hooks state