Skip to content

Commit a0991a9

Browse files
author
Christoph Pader
committed
fix: update useOnyx result when cache was set for the first time
1 parent 5da5e50 commit a0991a9

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

lib/useOnyx.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey
117117
// If `newValue` is `null` or any other value if means that the cache does have a value for that key.
118118
// This difference between `undefined` and other values is crucial and it's used to address the following
119119
// 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;
121121
const hasCacheForKey = cache.hasCacheForKey(key);
122122

123123
// 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
131131
newFetchStatus = 'loading';
132132
}
133133

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) {
139136
newFetchStatus = 'loaded';
140137
}
141138

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>;
144143
newFetchStatus = 'loaded';
145144
}
146145

147146
// If the previously cached value is different from the new value, we update both cached value
148147
// 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)) {
154151
cachedValueRef.current = newValue;
155152
resultRef.current = [cachedValueRef.current as CachedValue<TKey, TReturnValue>, {status: newFetchStatus ?? 'loaded'}];
156153
}

0 commit comments

Comments
 (0)