1 回答
TA贡献2051条经验 获得超10个赞
首先,让我们检查一切是否都呈现出来了:
row.map((cell, cindex) => {
console.log([cell, cindex], `rendered`);
return (
<td key={cindex}>
<textarea value={cell} onChange={handleChange} />
</td>
);
});
每次单元格更改时,都会呈现所有表。
我们可以用和一些修复关闭问题来修复它:React.memouseRef
const Entry = ({ value, rindex, cindex, onChange }) => {
console.log([rindex, cindex], 'rendered');
const textRef = useRef();
const $onChange = () => {
onChange(prev => {
let tempArr = [...prev];
tempArr[rindex][cindex] = textRef.current.value;
return tempArr;
});
};
return (
<td>
<textarea ref={textRef} value={value} onChange={$onChange} />
</td>
);
};
const MemoEntry = React.memo(Entry);
const Table = () => {
const [initData, setInitData] = useState([['a', 'b', 'c'], ['a', 'b', 'c']]);
return (
<>
<table>
<tbody>
{initData &&
initData.map((row, rindex) => (
<tr key={rindex}>
{row.map((cell, cindex) => {
return (
<MemoEntry
key={cindex}
rindex={rindex}
cindex={cindex}
value={cell}
onChange={setInitData}
/>
);
})}
</tr>
))}
</tbody>
</table>
</>
);
};
添加回答
举报