From 662c450c8fa9ac07fd13553ae5e6ecfc968e9cbb Mon Sep 17 00:00:00 2001 From: "harry.lang" Date: Fri, 27 Sep 2024 15:41:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Fixed=20#197=20=E4=BC=98=E5=8C=96useGlobalC?= =?UTF-8?q?ache=E4=B8=AD=E9=A2=91=E7=B9=81=E8=B0=83=E7=94=A8useEffectClean?= =?UTF-8?q?upRegister=E5=AF=BC=E8=87=B4=E5=86=85=E5=AD=98=E8=BF=87?= =?UTF-8?q?=E5=A4=9A=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 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hooks/useGlobalCache.tsx b/src/hooks/useGlobalCache.tsx index 93ab6c3..dfee42b 100644 --- a/src/hooks/useGlobalCache.tsx +++ b/src/hooks/useGlobalCache.tsx @@ -23,7 +23,8 @@ export default function useGlobalCache( ): CacheType { const { cache: globalCache } = React.useContext(StyleContext); const fullPath = [prefix, ...keyPath]; - const fullPathStr = pathKey(fullPath); + const stableFullPathStr = React.useRef(pathKey(fullPath)); + const fullPathStr = stableFullPathStr.current; const register = useEffectCleanupRegister([fullPathStr]); From 549e921a837b2e7ccd86fd9fe6d5cc44601e6ea0 Mon Sep 17 00:00:00 2001 From: "harry.lang" Date: Sun, 29 Sep 2024 16:48:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Fixed=20#197=20=E4=BC=98=E5=8C=96useGlobalC?= =?UTF-8?q?ache=E4=B8=AD=E9=A2=91=E7=B9=81=E8=B0=83=E7=94=A8useEffectClean?= =?UTF-8?q?upRegister=E5=AF=BC=E8=87=B4=E5=86=85=E5=AD=98=E8=BF=87?= =?UTF-8?q?=E5=A4=9A=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]);