我有一个n x n数组,其中包含用于呈现表单元格的所有数据。
const Table = () => {
const [initData, setInitData] = useState([
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"],
["a", "b", "c", "d", "e", "f", "b", "c", "d", "e", "f"]
]);
const handleChange = e => {
let thisTD = e.target.parentNode;
let cellIndex = thisTD.cellIndex;
let rowIndex = thisTD.parentNode.rowIndex;
let tempArr = [...initData];
tempArr[rowIndex][cellIndex] = e.target.value;
setInitData(tempArr);
};
return (
<>
<table>
<tbody>
{initData &&
initData.map((row, rindex) => (
<tr key={rindex}>
{row.map((cell, cindex) => (
<td key={cindex}>
<textarea value={cell} onChange={handleChange} />
</td>
))}
</tr>
))}
</tbody>
</table>
</>
);
};
问题是,每当单元格更改自己的数据时,表的所有单元格都将重新呈现。
如果单元格数量增加,则当用户更改单元格的内容时,这会导致性能降低和滞后。
目前,一切正常,但我想提高性能以进行缩放。在每次更改中重新呈现整个表似乎既愚蠢又昂贵,那么我该如何更改数据结构或我的 React 组件呢?感谢每一个想法!
侃侃无极
相关分类