我在这里做错了什么?
我从这个开始:
const [notificationList, setNotificationList] = useState([]);
然后填充数组。之后我想更新该数组中的每个对象......但不工作。
setNotificationList(notificationList.map(notif => { ...notif, isRead: true }))
我认为这很明显,但我需要睡觉。
您有语法错误。如果您运行代码,您应该会在控制台中看到它。
// this is the usual "old" syntax
function() {
return true
}
// this is the es6 arrow function
() => {
return true
}
// this is the es6 arrow function without braces
() => true
正如您在此处看到的,您可以{}
在箭头函数中省略。但是,如果您想返回一个对象,则必须明确,否则引擎无法理解_
// not what you want
() => { key: 'value' }
// what you want
() => ({ key: 'value' })
解决方案:
setNotificationList(notificationList.map(notif => ({ ...notif, isRead: true })))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句