Skip to content

Commit e1b8592

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

1 file changed

Lines changed: 2 additions & 10 deletions

File tree

lib/useOnyx.ts

Lines changed: 2 additions & 10 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,8 +220,6 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
226220
}, [key, options?.canEvict]);
227221

228222
const getSnapshot = useCallback(() => {
229-
const cacheKey = cacheKeyRef.current!;
230-
231223
// Check if we have any cache for this Onyx key
232224
// Don't use cache for first connection with initWithStoredValues: false
233225
// Also don't use cache during active data updates (when shouldGetCachedValueRef is true)
@@ -324,7 +316,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
324316
onyxSnapshotCache.setCachedResult<UseOnyxResult<TReturnValue>>(key, cacheKey, resultRef.current);
325317

326318
return resultRef.current;
327-
}, [options?.initWithStoredValues, options?.allowStaleData, options?.canBeMissing, key, memoizedSelector]);
319+
}, [options?.initWithStoredValues, options?.allowStaleData, options?.canBeMissing, key, memoizedSelector, cacheKey]);
328320

329321
const subscribe = useCallback(
330322
(onStoreChange: () => void) => {

0 commit comments

Comments
 (0)