From 549e921a837b2e7ccd86fd9fe6d5cc44601e6ea0 Mon Sep 17 00:00:00 2001 From: "harry.lang" Date: Sun, 29 Sep 2024 16:48:33 +0800 Subject: [PATCH] =?UTF-8?q?Fixed=20#197=20=E4=BC=98=E5=8C=96useGlobalCache?= =?UTF-8?q?=E4=B8=AD=E9=A2=91=E7=B9=81=E8=B0=83=E7=94=A8useEffectCleanupRe?= =?UTF-8?q?gister=E5=AF=BC=E8=87=B4=E5=86=85=E5=AD=98=E8=BF=87=E5=A4=9A?= =?UTF-8?q?=E5=8D=A0=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useGlobalCache.tsx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/hooks/useGlobalCache.tsx b/src/hooks/useGlobalCache.tsx index dfee42b..d9a37e7 100644 --- a/src/hooks/useGlobalCache.tsx +++ b/src/hooks/useGlobalCache.tsx @@ -23,8 +23,18 @@ export default function useGlobalCache( ): CacheType { const { cache: globalCache } = React.useContext(StyleContext); const fullPath = [prefix, ...keyPath]; + + // 缓存fullPathStr,减少render导致的内存占用问题 const stableFullPathStr = React.useRef(pathKey(fullPath)); - const fullPathStr = stableFullPathStr.current; + const fullPathStr = React.useMemo(() => { + const _fullPathStr = pathKey(fullPath); + // 比较fullPathStr变更 + if (_fullPathStr !== stableFullPathStr.current) { + stableFullPathStr.current = _fullPathStr; + return _fullPathStr; + } + return stableFullPathStr.current; + }, [fullPath]); const register = useEffectCleanupRegister([fullPathStr]);