reactjs - 使用不变性助手在特定索引处推送新对象
问题描述
我试图这样做:
const rowObj = {key: value};
const rowIndex = 2;
this.setState({
data: update(this.state.data,
{ [rowIndex] : { $push : [rowObj] } }
)
})
但是,它会抛出这样的错误:
Uncaught Error: update(): expected target of $push to be an array; got undefined.
解决方案
像这样试试
const rowArray = [{key: value},{key: value},{key: value}];
const obj = {key: value}
const rowIndex = 2;
rowArray.splice(rowIndex, 0, obj);
this.setState({ data: update(this.state.data,{rowArray : {$set : rowArray}} ) });
推荐阅读
- java - 如何在 Liferay 中复制现有的组织角色并将其添加到已经具有原始角色的用户中?
- javascript - 如何获取当前时间视频
- python - Writing values of a .csv file into dataframe as they are
- jasper-reports - Jasper:水平主报告导致错误“子报告在不支持溢出的带上溢出”
- c++ - 在tensorflow c ++中是否有将base64字符串解码为张量的函数?
- asp.net-core - Exceptions handled in UseExceptionHandler are logged by Application Insights, but they shouldnt
- javascript - Promise.all 和嵌套异步等待哪个更好?
- javascript - JavaScript Array.ptototype and for(vat value in myArray)
- reactjs - useEffect 中的异步函数获取旧道具
- .net - 我在 WPF 中有一个带有数据网格的树视图。我需要根据某些条件删除 datagridcell 值