diff --git a/lib/useOnyx.ts b/lib/useOnyx.ts index 92fe448b..56d5f19a 100644 --- a/lib/useOnyx.ts +++ b/lib/useOnyx.ts @@ -264,10 +264,10 @@ function useOnyx>( return result; } - // We get the value from cache while the first connection to Onyx is being made, - // so we can return any cached value right away. After the connection is made, we only + // We get the value from cache while the first connection to Onyx is being made or if the key has changed, + // 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 // update `newValueRef` when `Onyx.connect()` callback is fired. - if (isFirstConnectionRef.current || shouldGetCachedValueRef.current) { + if (isFirstConnectionRef.current || shouldGetCachedValueRef.current || key !== previousKey) { // Gets the value from cache and maps it with selector. It changes `null` to `undefined` for `useOnyx` compatibility. const value = OnyxUtils.tryGetCachedValue(key) as OnyxValue; const selectedValue = memoizedSelector ? memoizedSelector(value) : value;