File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed
Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments