reactjs - 使用不变性助手`update`时如何使用我的索引作为键
问题描述
我正在尝试使用react 文档中建议的immutability-helper更新我的列表,您可以看到我当前用于更新的代码
const oldInstallment = this.findInstallmentByIndex(this.state.installmentList, index);
let newInstallment = {...oldInstallment}
newInstallment.isActivated = isActivated;
const newInstallmentList = update(this.state.installmentList, {index: {$set: newInstallment}});
this.setState({installmentList: newInstallmentList});
我目前遇到的问题是,index
它没有用作值,而是用作键,命名index
含义而不是说明0:{$set: newInstallment}
代码的作用是index:{$set: newInstallment}
导致向数组中添加新元素而不是更新元素index 0
所以我的问题是如何我告诉这个更新方法使用我的索引值作为键?!
解决方案
试着这样写:
const newInstallmentList = update(this.state.installmentList, {[index]: {$set: newInstallment}});
推荐阅读
- json - 在 Scala 中使用递归字段解析基于模式的 JSON
- wordpress - Wordpress 中的 name 与 single_name 有什么区别?
- google-cloud-platform - 如何使用给定的服务帐户配置运行 gcloud 命令
- shell - 使用 Set -e ,如何使一个命令异常
- facebook - Facebook Instant Games API 方法 canSubscribeBotAsync() 在测试时总是返回 false
- assembly - 汇编语言混乱
- javascript - 防止动画期间的点击事件
- airflow - 气流 - 试图循环操作员。执行不是等待实际操作完成
- python - 如何在多索引数据框中折叠具有相同值的索引
- jquery - JQuery UI 上下文菜单选项