Skip to content

Commit

Permalink
Fixed #197 优化useGlobalCache中频繁调用useEffectCleanupRegister导致内存过多占用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
harry.lang committed Sep 29, 2024
1 parent 662c450 commit 549e921
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/hooks/useGlobalCache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ export default function useGlobalCache<CacheType>(
): 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]);

Expand Down

0 comments on commit 549e921

Please sign in to comment.