Skip to content

Commit 04c4123

Browse files
committed
Add isCollection deepEqual for cached results
1 parent 2ae6062 commit 04c4123

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/useOnyx.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
236236
if (!(isFirstConnectionRef.current && options?.initWithStoredValues === false) && !shouldGetCachedValueRef.current) {
237237
const cachedResult = onyxSnapshotCache.getCachedResult<UseOnyxResult<TReturnValue>>(key, cacheKey);
238238
if (cachedResult !== undefined) {
239+
// Collections are rebuilt as new objects each time via getCollectionData, so we need to compare values
240+
// and preserve reference stability to prevent unnecessary rerenders
241+
const isCollection = OnyxUtils.isCollectionKey(key);
242+
if (isCollection && deepEqual(cachedResult, resultRef.current)) {
243+
return resultRef.current;
244+
}
245+
239246
resultRef.current = cachedResult;
240247
return cachedResult;
241248
}

0 commit comments

Comments
 (0)