@@ -117,7 +117,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey
117
117
// If `newValue` is `null` or any other value if means that the cache does have a value for that key.
118
118
// This difference between `undefined` and other values is crucial and it's used to address the following
119
119
// conditions and use cases.
120
- let newValue = OnyxUtils . tryGetCachedValue ( key , { selector : selectorRef . current } ) as CachedValue < TKey , TReturnValue > | undefined ;
120
+ let newValue = ( OnyxUtils . tryGetCachedValue ( key , { selector : selectorRef . current } ) ?? undefined ) as CachedValue < TKey , TReturnValue > | undefined ;
121
121
const hasCacheForKey = cache . hasCacheForKey ( key ) ;
122
122
123
123
// Since the fetch status can be different given the use cases below, we define the variable right away.
@@ -131,26 +131,23 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey
131
131
newFetchStatus = 'loading' ;
132
132
}
133
133
134
- // If data is not present in cache and `initialValue` is set during the first connection,
135
- // we set the new value to `initialValue` and fetch status to `loaded` since we already have some data to return to the consumer.
136
- const hasInitialValue = isFirstConnectionRef . current && ! hasCacheForKey && options ?. initialValue !== undefined ;
137
- if ( hasInitialValue ) {
138
- newValue = options ?. initialValue as CachedValue < TKey , TReturnValue > ;
134
+ // If `options.initWithStoredValues` is explicitly set to `false`, the fetch status should always be `loaded`.
135
+ if ( options ?. initWithStoredValues === false ) {
139
136
newFetchStatus = 'loaded' ;
140
137
}
141
138
142
- // If `options.initiWithStoredValues` is explicitly set to `false`, the fetch status should always be `loaded`.
143
- if ( options ?. initWithStoredValues === false ) {
139
+ // If data is not present in cache and `initialValue` is set during the first connection,
140
+ // we set the new value to `initialValue` and fetch status to `loaded` since we already have some data to return to the consumer.
141
+ if ( isFirstConnectionRef . current && ! hasCacheForKey && options ?. initialValue !== null && options ?. initialValue !== undefined ) {
142
+ newValue = ( options ?. initialValue ?? undefined ) as CachedValue < TKey , TReturnValue > ;
144
143
newFetchStatus = 'loaded' ;
145
144
}
146
145
147
146
// If the previously cached value is different from the new value, we update both cached value
148
147
// and the result to be returned by the hook.
149
- if ( ! deepEqual ( cachedValueRef . current , newValue ) ) {
150
- // If there is no cached value yet, the value read from cache is `undefined` and the fetch status hasn't already been set,
151
- // we set the fetch status to `loading`.
152
- if ( ! newFetchStatus && cachedValueRef . current === null && ! hasCacheForKey ) newFetchStatus = 'loading' ;
153
-
148
+ // If the cache was set for the first time, we also update the cached value and the result.
149
+ const isCacheSetFirstTime = cachedValueRef . current === null && hasCacheForKey ;
150
+ if ( isCacheSetFirstTime || ! deepEqual ( cachedValueRef . current ?? undefined , newValue ) ) {
154
151
cachedValueRef . current = newValue ;
155
152
resultRef . current = [ cachedValueRef . current as CachedValue < TKey , TReturnValue > , { status : newFetchStatus ?? 'loaded' } ] ;
156
153
}
0 commit comments