react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

useQuery hook fetches data twice

Open Zver64 opened this issue 5 years ago • 10 comments

Intended outcome: Get data from graphql server provided by graphql documentation using useQuery hook from @apollo/react-hooks

Actual outcome: The data is fetched twice instead of once.

How to reproduce the issue: I have created a simple react app in codesandbox that you can open up using the link below: https://codesandbox.io/s/infallible-pasteur-wq3zp Here is the screenshot of my logs. You can see that the data object is returned twice: image

I have also tried to use apollo/client from beta docs, but the result is the same.

Version

System: OS: Windows 10 10.0.19587 Binaries: Node: 12.16.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: 44.19587.1000.0 npmPackages: apollo-boost: ^0.4.7 => 0.4.7

Zver64 avatar Apr 01 '20 17:04 Zver64

The console.log statement may be running twice, but your data is only being fetched once. If you look at the Network tab in your browser's dev tools then you'll see that there's only one request being made to 48p1r2roz4.sse.codesandbox.io. The reason the console.log statement is running twice is because you have it set up to call console.log every time TestComponent renders, and it renders twice after receiving data.

dylanwulf avatar Apr 01 '20 20:04 dylanwulf

Thank you for your responce @dylanwulf ! Is it a normal behavior for a component to render twice? I was expecting it to render once...

Zver64 avatar Apr 02 '20 09:04 Zver64

The extra render might not be necessary in your case, but apollo-client is probably updating some other values that you're not reading and that's why you see it render a second time. An extra render will not affect the appearance of your component at all, and should be negligible in terms of performance.

dylanwulf avatar Apr 02 '20 14:04 dylanwulf

It looks like it only works because of caching...If I try to send a unique request for each render - I end up having an infinite number of requests. And I also found out that I cannot disable cache by setting no-cache fetch policy.

Zver64 avatar Apr 04 '20 14:04 Zver64

Same issue for me as well, I am able to see four console logs

image

and last one is

image

Nisfan avatar Apr 09 '20 11:04 Nisfan

I'm seeing duplicate renders as well. Both renders happen after query loading has finished.

dane-stevens avatar May 28 '20 19:05 dane-stevens

Isn't this problem related to <React.StrictMode>?

sirldev avatar Jun 01 '20 04:06 sirldev

@JunHyeokHa it is indeed! Thank you!

dane-stevens avatar Jun 01 '20 11:06 dane-stevens

Any workaround for this?. rendering twice is causing issues in infinite scroll

mohammed-shameem avatar Jun 05 '20 23:06 mohammed-shameem

@mohammed-shameem As @JunHyeokHa mentioned the problem is related to React Strict mode. So you can disable it, if it is not necessary for your project.

Zver64 avatar Jun 18 '20 18:06 Zver64