浏览代码

🐛修复表格拖拽闪动问题,使用useCallback函数

shaogen1995 2 年之前
父节点
当前提交
9215de7b94
共有 1 个文件被更改,包括 48 次插入42 次删除
  1. 48 42
      src/pages/Wall/WallTable/index.tsx

+ 48 - 42
src/pages/Wall/WallTable/index.tsx

@@ -161,49 +161,55 @@ function WallTable() {
 
   const type = "DraggableBodyRow";
 
-  const DraggableBodyRow = ({
-    index,
-    moveRow,
-    className,
-    style,
-    ...restProps
-  }: DraggableBodyRowProps) => {
-    const ref = useRef<HTMLTableRowElement>(null);
-    const [{ isOver, dropClassName }, drop] = useDrop({
-      accept: type,
-      collect: (monitor) => {
-        const { index: dragIndex } = monitor.getItem() || {};
-        if (dragIndex === index) {
-          return {};
-        }
-        return {
-          isOver: monitor.isOver(),
-          dropClassName:
-            dragIndex < index ? " drop-over-downward" : " drop-over-upward",
-        };
-      },
-      drop: (item: { index: number }) => {
-        moveRow(item.index, index);
-      },
-    });
-    const [, drag] = useDrag({
-      type,
-      item: { index },
-      collect: (monitor) => ({
-        isDragging: monitor.isDragging(),
-      }),
-    });
-    drop(drag(ref));
+  const DraggableBodyRow = useCallback(
+    ({
+      index,
+      moveRow,
+      className,
+      style,
+      ...restProps
+    }: DraggableBodyRowProps) => {
+      // eslint-disable-next-line react-hooks/rules-of-hooks
+      const ref = useRef<HTMLTableRowElement>(null);
+      // eslint-disable-next-line react-hooks/rules-of-hooks
+      const [{ isOver, dropClassName }, drop] = useDrop({
+        accept: type,
+        collect: (monitor) => {
+          const { index: dragIndex } = monitor.getItem() || {};
+          if (dragIndex === index) {
+            return {};
+          }
+          return {
+            isOver: monitor.isOver(),
+            dropClassName:
+              dragIndex < index ? " drop-over-downward" : " drop-over-upward",
+          };
+        },
+        drop: (item: { index: number }) => {
+          moveRow(item.index, index);
+        },
+      });
+      // eslint-disable-next-line react-hooks/rules-of-hooks
+      const [, drag] = useDrag({
+        type,
+        item: { index },
+        collect: (monitor) => ({
+          isDragging: monitor.isDragging(),
+        }),
+      });
+      drop(drag(ref));
 
-    return (
-      <tr
-        ref={ref}
-        className={`${className}${isOver ? dropClassName : ""}`}
-        style={{ cursor: "move", ...style }}
-        {...restProps}
-      />
-    );
-  };
+      return (
+        <tr
+          ref={ref}
+          className={`${className}${isOver ? dropClassName : ""}`}
+          style={{ cursor: "move", ...style }}
+          {...restProps}
+        />
+      );
+    },
+    []
+  );
 
   const components = {
     body: {