Skip to content

Commit 96a9061

Browse files
committed
properly expose hasComputed flag
1 parent b9e24a2 commit 96a9061

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

lib/useOnyx.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,11 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
8080

8181
const currentDependenciesRef = useLiveRef(dependencies);
8282
const selector = options?.selector;
83-
const memoizedSelectorRef = useRef<[typeof selector | null, boolean]>([null, true]);
8483

8584
// Create memoized version of selector for performance
86-
memoizedSelectorRef.current = useMemo(() => {
85+
const [memoizedSelector, getHasMemoizedSelectorComputed] = useMemo((): [UseOnyxSelector<TKey, TReturnValue> | null, () => boolean] => {
8786
if (!selector) {
88-
return [null, true];
87+
return [null, () => true];
8988
}
9089

9190
let lastInput: OnyxValue<TKey> | undefined;
@@ -116,7 +115,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
116115

117116
return lastOutput;
118117
},
119-
hasComputed,
118+
() => hasComputed,
120119
];
121120
}, [currentDependenciesRef, selector]);
122121

@@ -257,11 +256,10 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
257256
return result;
258257
}
259258

260-
const [memoizedSelector, hasMemoizedSelectorComputed] = memoizedSelectorRef.current;
261259
// We get the value from cache while the first connection to Onyx is being made or if the key has changed,
262260
// so we can return any cached value right away. For the case where the key has changed, If we don't return the cached value right away, then the UI will show the incorrect (previous) value for a brief period which looks like a UI glitch to the user. After the connection is made, we only
263261
// update `newValueRef` when `Onyx.connect()` callback is fired.
264-
if (isFirstConnectionRef.current || shouldGetCachedValueRef.current || key !== previousKey || !hasMemoizedSelectorComputed) {
262+
if (isFirstConnectionRef.current || shouldGetCachedValueRef.current || key !== previousKey || !getHasMemoizedSelectorComputed()) {
265263
// Gets the value from cache and maps it with selector. It changes `null` to `undefined` for `useOnyx` compatibility.
266264
const value = OnyxUtils.tryGetCachedValue(key) as OnyxValue<TKey>;
267265
const selectedValue = memoizedSelector ? memoizedSelector(value) : value;
@@ -336,7 +334,8 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
336334
}
337335

338336
return resultRef.current;
339-
}, [options?.initWithStoredValues, options?.allowStaleData, options?.canBeMissing, key, memoizedSelectorRef, cacheKey, previousKey]);
337+
// eslint-disable-next-line react-hooks/exhaustive-deps
338+
}, [options?.initWithStoredValues, options?.allowStaleData, options?.canBeMissing, key, memoizedSelector, cacheKey, previousKey]);
340339

341340
const subscribe = useCallback(
342341
(onStoreChange: () => void) => {

0 commit comments

Comments
 (0)