藍(lán)山帝景
2021-10-14 10:10:16
我的setState功能無(wú)法正常工作,因?yàn)樗鼪](méi)有呈現(xiàn)組件。設(shè)置重現(xiàn)。選擇任何區(qū)域。示例見(jiàn)下圖第一行第二列。查看控制臺(tái)上的更新行數(shù)據(jù)?,F(xiàn)在我正在嘗試使用更新的數(shù)據(jù)更新視圖,但它沒(méi)有反映。這是我的代碼 https://codesandbox.io/s/rdg-cell-editing-4cu5p如果onUpdate: args => { const { startCell, topLeft, bottomRight } = args; const { idx: topLeftColumn, rowIdx: topLeftRowIdx } = topLeft; const { idx: startColumn, rowIdx: startRowIndex } = startCell; const { idx: bottomColumn, rowIdx: bottomRowIndex } = bottomRight; const rows = this.state.rows.slice(); console.log(this.state.rows[startRowIndex][keys[startColumn]]); let item = this.state.rows[startRowIndex][keys[startColumn]]; console.log(topLeftRowIdx, "topLeftRowIdx"); console.log(bottomRowIndex, "bottomRowIndex"); console.log(topLeftColumn, "topLeftColumn"); console.log(bottomColumn, "bottomColumn"); for (var i = topLeftRowIdx; i <= bottomRowIndex; i++) { for (var j = topLeftColumn; j <= bottomColumn; j++) { console.log("----"); rows[i][keys[j]] = item; } } console.log(rows); this.setState({ rows: [...rows] }); // this.setState({ // rows:[ // { id: 0, title: "Task 1ssss", complete: "Task 2" }, // { id: 1, title: "Task 2", complete: "Task 3" }, // { id: 2, title: "Task 3", complete: "Task 4" } // ] // }); console.log(this.state); return rows; },如果我做硬編碼setState,這是注釋掉的代碼,那么它就可以工作// this.setState({ // rows:[ // { id: 0, title: "Task 1ssss", complete: "Task 2" }, // { id: 1, title: "Task 2", complete: "Task 3" }, // { id: 2, title: "Task 3", complete: "Task 4" } // ] // });上面的代碼在選擇區(qū)域后工作,當(dāng)我取消注釋代碼時(shí)它會(huì)更新視圖
1 回答

慕仙森
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
改為數(shù)組深拷貝:
從
this.setState({
rows: [...rows]
});
到
this.setState({
rows: JSON.parse(JSON.stringify(rows))
});
在循環(huán)中復(fù)制項(xiàng)目的更有效方法:
for (var i = topLeftRowIdx; i <= bottomRowIndex; i++) {
for (var j = topLeftColumn; j <= bottomColumn; j++) {
console.log("----");
rows[i][keys[j]] = item;
}
rows[i] = { ...rows[i]};
}
添加回答
舉報(bào)
0/150
提交
取消