reactjs - 优化 reactjs 中的拖动单元格表?
问题描述
我正在建一张桌子。当用户单击一个单元格并拖过其他单元格时。之间的单元格必须突出显示。但是在我的实际项目中有 1000 个单元格,它太慢了,无论如何都要优化我的代码。
编码
export default function Table() {
const [start, setStart] = useState(null);
const [end, setEnd] = useState(0);
const [selecting, setSelecting] = useState(false);
const [isOpen, setIsOpen] = useState(false);
let toggleModal = () => {
setIsOpen(!isOpen);
};
let beginSelection = i => {
setSelecting(true);
setStart(i);
setEnd(i);
updateSelection(i);
};
let endSelection = (i = end) => {
setSelecting(false);
updateSelection(i);
};
let updateSelection = i => {
if(selecting) {
setEnd(i);
}
};
let cells = [];
for(let j = 0; j < 12*4; j++) {
cells.push(
<Cell key={j}
inputColor={
(end <= j && j <= start || (start <= j && j <= end) ? "#adf": "")
}
onMouseDown={()=>beginSelection(j)}
onMouseUp={()=>endSelection(j)}
onMouseMove={()=> updateSelection(j)}
>
{j+1}
</Cell>
)
}
return (
<TableCalendar>
{cells}
</TableCalendar>
)
}
这是我的完整代码和演示:https ://codesandbox.io/s/elastic-noether-4krei
谢谢!
解决方案
推荐阅读
- arrays - Ruby - 我在将数组中的特定单词大写时遇到困难,这些单词将作为字符串返回
- scala - Scala快速排序算法实现
- javascript - React:如何将来自钩子的返回值包装在 HTML 元素中,例如 span
- visual-studio-code - 将输入字符串映射到 launch.json 文件中的数值
- typescript - 如何使用黄瓜和类型脚本处理量角器中弹出的窗口身份验证
- python - 如何在屏幕上从左到右为对象设置动画而不拖尾?
- scala - 在数组中查找具有最多唯一字符的字符串
- java - 如何使用 Java、Node.js 和 MySQL 将 png 文件发送到 node.js 服务并将其存储在数据库中?
- android-studio - Jetpack compose 的 Android Canary 依赖项
- chart.js - ChartJS:自动缩放到最高值