Skip to content

Commit a5e7f42

Browse files
committed
memoize cache key
1 parent 9c1067d commit a5e7f42

1 file changed

Lines changed: 2 additions & 9 deletions

File tree

lib/useOnyx.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
149149
const sourceValueRef = useRef<NonNullable<TReturnValue> | undefined>(undefined);
150150

151151
// Cache the options key to avoid regenerating it every getSnapshot call
152-
const cacheKeyRef = useRef<string | null>(null);
153-
154-
// Generate cache key and update when options change
155-
const currentCacheKey = onyxSnapshotCache.generateCacheKey(options);
156-
if (cacheKeyRef.current !== currentCacheKey) {
157-
cacheKeyRef.current = currentCacheKey;
158-
}
152+
const cacheKey = useMemo(() => onyxSnapshotCache.generateCacheKey(options), [options]);
159153

160154
useEffect(() => {
161155
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
@@ -226,7 +220,6 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
226220
}, [key, options?.canEvict]);
227221

228222
const getSnapshot = useCallback(() => {
229-
const cacheKey = cacheKeyRef.current!;
230223

231224
// Check if we have any cache for this Onyx key
232225
// Don't use cache for first connection with initWithStoredValues: false
@@ -324,7 +317,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
324317
onyxSnapshotCache.setCachedResult<UseOnyxResult<TReturnValue>>(key, cacheKey, resultRef.current);
325318

326319
return resultRef.current;
327-
}, [options?.initWithStoredValues, options?.allowStaleData, options?.canBeMissing, key, memoizedSelector]);
320+
}, [options?.initWithStoredValues, options?.allowStaleData, options?.canBeMissing, key, memoizedSelector, cacheKey]);
328321

329322
const subscribe = useCallback(
330323
(onStoreChange: () => void) => {

0 commit comments

Comments
 (0)