Przeglądaj źródła

perf: 优化useAutoTable

lanjianrong 5 lat temu
rodzic
commit
97af198b18
1 zmienionych plików z 4 dodań i 6 usunięć
  1. 4 6
      src/hooks/useAutoTable.js

+ 4 - 6
src/hooks/useAutoTable.js

@@ -6,15 +6,13 @@ const useAutoTable = (collapsed) => {
   const needSubtractWidth = (collapsed ? 208 : 48) + 48 + 48 + 48// 需要被裁掉的高度
   const needSubtractWidth = (collapsed ? 208 : 48) + 48 + 48 + 48// 需要被裁掉的高度
 
 
 
 
-  const [scroll, setScroll] = useState({ x: 0, y: 0 })
+  const [scroll, setScroll] = useState({ x: document.body.clientWidth - needSubtractWidth, y: document.body.clientHeight - needSubtractHeight })
 
 
   useEffect(() => {
   useEffect(() => {
-    setScroll({ ...scroll, x: document.body.clientWidth - needSubtractWidth, y: document.body.clientHeight - needSubtractHeight })
-    window.addEventListener('resize', () => {
-      setScroll({ ...scroll, x: document.body.clientWidth - needSubtractWidth, y: document.body.clientHeight - needSubtractHeight })
-    })
+    const handleScroll = () => setScroll({ ...scroll, x: document.body.clientWidth - needSubtractWidth, y: document.body.clientHeight - needSubtractHeight })
+    window.addEventListener('resize', handleScroll)
     return () => {
     return () => {
-      window.removeEventListener('resize', () => { })
+      window.removeEventListener('resize', handleScroll)
     }
     }
   }, [collapsed])
   }, [collapsed])