Skip to content

Commit e5e34c2

Browse files
authored
Merge pull request #708 from nkdengineer/fix/76015
fix: result isn't changed when the member collection change
2 parents e7f1392 + ca24c78 commit e5e34c2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/useOnyx.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
122122
// Stores the newest cached value in order to compare with the previous one and optimize `getSnapshot()` execution.
123123
const newValueRef = useRef<TReturnValue | undefined | null>(null);
124124

125+
const lastConnectedKeyRef = useRef<TKey>(key);
126+
125127
// Stores the previously result returned by the hook, containing the data from cache and the fetch status.
126128
// We initialize it to `undefined` and `loading` fetch status to simulate the initial result when the hook is loading from the cache.
127129
// However, if `initWithStoredValues` is `false` we set the fetch status to `loaded` since we want to signal that data is ready.
@@ -162,6 +164,16 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
162164

163165
useEffect(() => () => onyxSnapshotCache.deregisterConsumer(key, cacheKey), [key, cacheKey]);
164166

167+
useEffect(() => {
168+
if (lastConnectedKeyRef.current === key) {
169+
return;
170+
}
171+
lastConnectedKeyRef.current = key;
172+
shouldGetCachedValueRef.current = true;
173+
previousValueRef.current = null;
174+
resultRef.current = [undefined, {status: options?.initWithStoredValues === false ? 'loaded' : 'loading'}];
175+
}, [key, options?.initWithStoredValues]);
176+
165177
useEffect(() => {
166178
// These conditions will ensure we can only handle dynamic collection member keys from the same collection.
167179
if (options?.allowDynamicKey || previousKey === key) {

0 commit comments

Comments
 (0)