渲染大型桌子时的性能问题
我正在创建一个日历组件,用于使用HTML表调度,我希望能够拖动类似于Google Cal的事件。当我拖动元素时,我希望使用Ondragenter处理程序拖动的单元格背景。我是通过将标识符保存到一个状态下的单元格中,然后在单元格上制作背景样式来做到这一点。
但是,背景更新似乎落后于用户,因为该表非常大,并且由于映射了单元格,因此正在重新构建整个表。见下文。
请参阅下面的代码。
const [dragCellNum, setDragCellNum] = useState(null);
const handleDragIn = (e) => {
e.preventDefault();
e.stopPropagation();
setDragCellNum(e.target.id);
};
// simplified return
return
<table>
<tbody>
// Rows are mapped as well.
<tr>
// Mapping all the cells here
{data.map(x=>
<td
key={id}
id={id}
style={{
// Here is conditional BG color change
backgroundColor: id === dragCellNum ? '#CCCCCC' : #FFFFFF,
}}
onDragEnter={handleDragIn}
>
)}
</tr>
</tbody>
</table>
为了简单起见,我遗漏了很多东西,但是桌子很大。从本质上讲,我想知道是否有一种方法可以使用React Memo或Ref或完全不同的方法来防止整个桌子重新供应。
谢谢大家。
I am creating a calendar component for scheduling using an HTML table and I want to be able to drag around an event similar to google cal. When I drag the element, I would like the background of the cell i am dragging over to change using the onDragEnter handler. I do this by saving an identifier to the cell in a state and then making the background style on the cell conditional.
However, the background update appears to lag behind the user because the table is very large and since the cells are mapped, it is re-rendering the whole table. See below.
See the code below.
const [dragCellNum, setDragCellNum] = useState(null);
const handleDragIn = (e) => {
e.preventDefault();
e.stopPropagation();
setDragCellNum(e.target.id);
};
// simplified return
return
<table>
<tbody>
// Rows are mapped as well.
<tr>
// Mapping all the cells here
{data.map(x=>
<td
key={id}
id={id}
style={{
// Here is conditional BG color change
backgroundColor: id === dragCellNum ? '#CCCCCC' : #FFFFFF,
}}
onDragEnter={handleDragIn}
>
)}
</tr>
</tbody>
</table>
I left out a lot for simplicity's sake, but the table is large. Essentially, I am wondering if there is a way to use react memo or ref or a whole different way to do this to keep the entire table from re-rendering.
Thanks, everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论