Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTK-Query: Stop polling when window looses focus #2339

Closed
Brookke opened this issue May 20, 2022 · 3 comments
Closed

RTK-Query: Stop polling when window looses focus #2339

Brookke opened this issue May 20, 2022 · 3 comments

Comments

@Brookke
Copy link

Brookke commented May 20, 2022

Hello, is there a built in way in RTK-Query to disable polling when the window looses focus?

@kennysliding
Copy link

kennysliding commented Sep 26, 2022

A proposed solution now is to use the skip flag, we can use an hook to get the state of whether the window is in focus, then pass that boolean into the query,

const isWindowFocused = useWindowFocus();
const { data, error, isLoading } = usePlaceHolderQuery(
    {},
    { pollingInterval: 5000, skip: isWindowFocused }
);

And the query will be skipped once the window is not focus

@phryneas
Copy link
Member

Or

const isWindowFocused = useWindowFocus();
const { data, error, isLoading } = usePlaceHolderQuery(
    {},
    { pollingInterval: isWindowFocused ? 5000 : undefined }
);
  • this will only stop polling, while skip might get the data removed from the cache over time.

@stevenlafl
Copy link

stevenlafl commented Jul 17, 2023

https://redux-toolkit.js.org/rtk-query/usage/queries#query-hook-options

const { data, error, isLoading } = usePlaceHolderQuery(null, {
  refetchOnFocus: false
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants