Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/useOnyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@
return result;
}

// We get the value from cache while the first connection to Onyx is being made,
// We get the value from cache while the first connection to Onyx is being made or the key has changed,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we return the cached value if the key has changed? Can we include the reason in this comment?

Suggested change
// We get the value from cache while the first connection to Onyx is being made or the key has changed,
// 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. 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like there are already a handful of options that should help the original issue, without making this change. useOnyx() has options for:

  • initWithStoredValues
  • allowStaleData
  • allowDynamicKey
  • selectors with a dependency array

Wouldn't there be some combination of those that would fix the original problem in the issue?

// 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<TKey>;
const selectedValue = memoizedSelector ? memoizedSelector(value) : value;
Expand Down Expand Up @@ -330,7 +330,7 @@

// If `canBeMissing` is set to `false` and the Onyx value of that key is not defined,
// we log an alert so it can be acknowledged by the consumer. Additionally, we won't log alerts
// if there's a `Onyx.clear()` task in progress.

Check warning on line 333 in lib/useOnyx.ts

View workflow job for this annotation

GitHub Actions / lint

React Hook useCallback has a missing dependency: 'previousKey'. Either include it or remove the dependency array
if (options?.canBeMissing === false && newFetchStatus === 'loaded' && !isOnyxValueDefined && !OnyxCache.hasPendingTask(TASK.CLEAR)) {
Logger.logAlert(`useOnyx returned no data for key with canBeMissing set to false for key ${key}`, {showAlert: true});
}
Expand Down