您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

ReactJS更新状态数组中的单个对象

ReactJS更新状态数组中的单个对象

您可以使用react 不变性助手

从文档:

var initialArray = [1, 2, 3];
var newArray = update(initialArray, {$push: [4]}); // => [1, 2, 3, 4]

initialArray仍为[1、2、3]。

因此,对于您的示例,您将需要执行以下操作:

if (index !== -1) {
    var deviceWithMods = {}; // do your stuff here
    this.setState(update(this.state.devices, {index: {$set: deviceWithMods }}));
}

根据device模型的复杂程度,您可以就地“修改”对象属性

if (index !== -1) {
    this.setState(update(this.state.devices[index], {name: {$set: 'a new device name' }}));
}
其他 2022/1/1 18:14:41 有639人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶