From f0124c61f3b911a934f6cb28e018283dc2df1a91 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 4 Dec 2025 15:20:33 +0700 Subject: [PATCH] fix: result isn't changed when the member collection change --- lib/useOnyx.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/useOnyx.ts b/lib/useOnyx.ts index 016eb0f3..1757936c 100644 --- a/lib/useOnyx.ts +++ b/lib/useOnyx.ts @@ -121,6 +121,8 @@ function useOnyx>( // Stores the newest cached value in order to compare with the previous one and optimize `getSnapshot()` execution. const newValueRef = useRef(null); + const lastConnectedKeyRef = useRef(key); + // Stores the previously result returned by the hook, containing the data from cache and the fetch status. // We initialize it to `undefined` and `loading` fetch status to simulate the initial result when the hook is loading from the cache. // However, if `initWithStoredValues` is `false` we set the fetch status to `loaded` since we want to signal that data is ready. @@ -161,6 +163,16 @@ function useOnyx>( useEffect(() => () => onyxSnapshotCache.deregisterConsumer(key, cacheKey), [key, cacheKey]); + useEffect(() => { + if (lastConnectedKeyRef.current === key) { + return; + } + lastConnectedKeyRef.current = key; + shouldGetCachedValueRef.current = true; + previousValueRef.current = null; + resultRef.current = [undefined, {status: options?.initWithStoredValues === false ? 'loaded' : 'loading'}]; + }, [key, options?.initWithStoredValues]); + useEffect(() => { // These conditions will ensure we can only handle dynamic collection member keys from the same collection. if (options?.allowDynamicKey || previousKey === key) {