tea-design / tea-component

37 stars 6 forks source link

路由切换时,table组件会报错, #14

Closed muzhichu closed 2 years ago

muzhichu commented 2 years ago

版本:"tea-component": "2.7.6" 报错信息如下:

Can’t perform a React state update on an unmounted component. This is
a no-op, but it indicates a memory leak in your application. To fix,
cancel all subscriptions and asynchronous tasks in a useEffect cleanup
function.

原因是tea-component/es/_util/use-resize-observer.js这个文件在页面卸载后还会进行setState操作, 由于组件没有开源提不了PR,现把我用patch-package打补丁解决的代码

--- a/node_modules/tea-component/es/_util/use-resize-observer.js
+++ b/node_modules/tea-component/es/_util/use-resize-observer.js
@@ -1,12 +1,17 @@
 import { __read } from "tslib";
-import { useEffect, useState } from "react";
+import { useEffect, useState, useRef } from "react";
 import ResizeObserver from "resize-observer-polyfill";
 export function useResizeObserver(ref) {
+    var isUnmount = useRef(null)
     var _a = __read(useState({}), 2), size = _a[0], setSize = _a[1];
     useEffect(function () {
         if (ref.current) {
             var resizeObserver_1 = new ResizeObserver(function (entries) {
                 requestAnimationFrame(function () {
+                    if(isUnmount.current) {
+                      return
+                    }
                     if (!Array.isArray(entries) || !entries.length) {
                         return;
                     }
@@ -16,7 +21,10 @@ export function useResizeObserver(ref) {
                 });
             });
             resizeObserver_1.observe(ref.current);
-            return function () { return resizeObserver_1.disconnect(); };
+            return function () {
+              isUnmount.current = true
+              return resizeObserver_1.disconnect();
+            };
         }
         return function () { };
     }, [ref]);

大佬有空可以看下能不能修复,

xughv commented 2 years ago

@JackieDark 看看

loongcheung commented 2 years ago

@muzhichu 感谢反馈,我们在tea-component@2.7.7-beta.1版本优化修复了这个问题,可以验证下,谢谢

muzhichu commented 2 years ago

@muzhichu 感谢反馈,我们在tea-component@2.7.7-beta.1版本优化修复了这个问题,可以验证下,谢谢

验证OK了,效率无敌,大佬们辛苦了