Skip to content

Multiple consumers of the same cache key all paused if only one isPaused() evaluates to true #2333

@Jackman3005

Description

@Jackman3005

Bug report

Description / Observed Behavior

We are using SWR in a react-native application with react-navigation. React Navigation stack navigator allows multiple screens to be mounted with one of them (the top of the stack) actually being focused. This allows quick switching to a previous screen without re-rendering from scratch.

In order to avoid SWR reloading data for all screens in the stack whenever a revalidate event is triggered (focus, etc.) we were hoping to use isPaused() like this:

const isFocused = useIsFocused();

const { data, error, mutate, isValidating } = useSWR(["get", "/api/123"], apiRequest, {
  isPaused: () => {
    return !isFocused;
  },
});

What's the problem?

When two screens are loading data for the same cache key, one may be focused and the other would not be focused. In this situation SWR is not making a request to load data for the focused screen. We know it is the isPaused that is causing the issue because if we remove the isPaused config in the un-focused screen everything loads fine.

I have also confirmed that isPaused is called for both usages of the hook, each screen evaluating to the expected value based on its focus state.

Expected Behavior

I expect useSWR to load data and keep it up-to-date when isPaused evaluates to false regardless of if another hook in use for the same cache key has an isPaused that evaluates to true. I only want something to be paused if all hooks consuming the same cache key evaluate isPaused to true.

Repro Steps / Code Example

Hopefully the code and scenario given above gives a bit of insight into a repro. Feel free to request me to make a repro if you think it is important.

Additional Context

Issue originally discovered on SWR 1.3.0 but I am able to reproduce it just the same on 2.0.0.

Thanks in advance for taking a look!
Jack (& The Questmate Team 🚀)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions