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

Unexpected Data Fetching with useBackgroundQuery, StrictMode, fetchPolicy: network-only (or no-cache), and state changes #11815

Closed
wand2016 opened this issue Apr 27, 2024 · 5 comments · Fixed by #11821

Comments

@wand2016
Copy link

Issue Description

When utilizing useBackgroundQuery alongside React StrictMode and setting fetchPolicy to network-only or no-cache, coupled with state changes within the same component as the useBackgroundQuery call, it leads to the creation of a new queryRef instance. Consequently, this triggers undesired refetching behavior.

Link to Reproduction

https://codesandbox.io/p/sandbox/cool-shadow-3nm9s9?file=%2Fsrc%2FApp.js%3A58%2C35

Reproduction Steps

  1. Click the increment %d button.
  2. Upon clicking, the Grandchild component is re-rendered with a new queryRef instance, resulting in undesired fetching.
cool-shadow-3nm9s9.-.CodeSandbox.-.Google.Chrome.2024-04-27.23-00-30.mp4

Note

disabling StrictMode fixes the issue.

cool-shadow-3nm9s9.-.CodeSandbox.-.Google.Chrome.2024-04-27.23-03-39.mp4

Expected Behavior

The useBackgroundQuery hook should not create a new queryRef instance upon state changes within the same component, as long as it does not alter the options of the hook call.

Actual Behavior

The combination of the mentioned factors leads to the creation of a new queryRef instance, resulting in unexpected fetching behavior.

@apollo/client version

3.10.1

@bignimbus
Copy link
Contributor

Hi @misacorn 👋🏻 thanks for reporting this and for providing a reproduction! We'll take a look at figure out next steps.

@jerelmiller
Copy link
Member

Hey @wand2016 👋

I've got a fix for this with #11821 and can confirm it fixes the issue given your reproduction. If you want to try this out for yourself, you can install the snapshot release with:

npm i @apollo/client@0.0.0-pr-11821-20240501000021

We'll try and get this out in the next version!

@wand2016
Copy link
Author

wand2016 commented May 1, 2024

@jerelmiller Thank you so much! This fix is a lifesaver for our product.

@jerelmiller
Copy link
Member

@wand2016 Awesome! If you need a temporary fix until we get this out, you should be able to downgrade to 3.9.9 which doesn't have this issue. It was introduced in 3.9.10. Not sure if you depend on any of the functionality in 3.10 but wanted to give you this option as well! I'm hoping we can get this patch out quickly though so you don't have to do this 🙂

Copy link
Contributor

github-actions bot commented May 3, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

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

Successfully merging a pull request may close this issue.

3 participants