Skip to content

Query with refetchOnFocus in combination with redux-persist: data is not refetched upon app launch after hard close #4259

Closed
@fikkatra

Description

@fikkatra

We are using redux-toolkit in combination with redux-persist in a React Native app. We use it to keep the user's data available when they are offline.

We pass the following options to the query hook:

{
  refetchOnFocus: true,
  refetchOnReconnect: true,
}

To make this work in React Native, we pass a custom callback to setupListeners as mentioned here.

Once the data has been fetched from the backend, it's persisted in async storage. So far so good. When the user launches the app, the expected behavior is as follows:

  • If the device is offline, the persisted data is shown
  • If the user is online, the data is refetched. The previously persisted data may show briefly while the refetch is in progress, but eventually, the updated data should be shown (and persisted).

While the first case (offline) works nicely, the second doesn't: the persisted data is shown but the data is never updated from the backend. We observe this behavior when fully initializing the app, e.g. after a hard close. We see the following in the logs:

  • onFocus is dispatched from setupListeners callback
  • REHYDRATE action is received, containing the previously persisted data
  • no backend calls were made
  • the data remains stale :-(

I'd expect that onFocus would trigger a backend call, but it doesn't.

When we deactivate the app and open it again (without hard-closing it), we do see the desired behavior:

  • onFocus is dispatched from setupListeners callback
  • no REHYDRATE action is received
  • a backend request is fired
  • the data is updated

Of course, we want it to display the updated data, regardless if the app was hard or soft closed.

One extra thing worth mentioning: we are using react-navigation, which keeps screens mounted even though they are not focused. This implies there is always an active query subscription.

I have a feeling I'm doing something wrong, but I'm not sure what. What am I missing here?

@reduxjs/[email protected]
[email protected]
[email protected]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions