【发布时间】:2019-03-28 18:21:50
【问题描述】:
我有两个具有相同对象属性的数组,我想只更新一个数组中的特定对象数据而不影响第二个对象数组。
我尝试过 JSON 解析,但它不起作用,因为我在数据上使用拖放操作,所以我想更新特定的对象传递索引。
this.state = {
listItem: [
// This data will be static
{ data: `Text`, content: 'Insert you text here...' },
{
data: `Column`,
content: 'Column',
columns: [
{
column: []
},
{
column: []
}
]
}
],
layout:[
// The data below might be more than 3-4 times repetitive
{
data: `Column`,
content: 'Column',
columns: [
{
column: []
},
{
column: []
}
]
}
]
}
// Set State function I tried recently
onColumnDrop(e, layoutIndex, colIndex) {
if (e.removedIndex !== null || e.addedIndex !== null) {
var stateCopy = Object.assign({}, this.state.layout);
stateCopy[layoutIndex].columns[colIndex].column = e.payload;
this.setState({ stateCopy });
}
}
所以,基本上功能是当我从 listItem 拖动对象并放入布局列数组中时,所以我想在 column[0] 或 column[1] 数组中设置拖动的对象,所以发生的事情是当我继续推动listItem[0] 在 column[0] 或 column[1] 数组中,然后在 listItem 列中同时更新,不知道为什么!但我超级困惑。
【问题讨论】:
-
你能分享一下调用 setState 的代码吗?
-
@Simran 这是我用于 setState 的函数:
codeonColumnDrop(e, layoutIndex, colIndex) { if (e.removedIndex !== null || e.addedIndex !== null) { var stateCopy = Object.assign({}, this.state.layout); stateCopy[layoutIndex].columns[colIndex].colum = e.payload; this.setState({ stateCopy }); } }code -
请添加到问题中,难以阅读 cmets 中的代码。
-
@Simran 在问题中添加了相同的内容,请查看。谢谢
-
你能花点时间正确缩进你的代码吗? @PravinGaikwad
标签: javascript reactjs react-redux setstate