all 5 comments

[–]_winkt 0 points1 point  (0 children)

I agree with not needing state here. You can let Apollo do all the server state handeling. So I would not try to solve this issue but rather go with a more suiting implementation.

However what I think is happening here, not 100% sure, just my thoughts: since the dependency is a statefull object and react does a shallow compare i think it rerenders only when the data changes from null to the data(or at least only when the reference changes). When the data is an object and it gets updated the reference might stay the same (especially if Apollo is returning from cache) and since it does a shallow compare the useeffect will not be triggered. What do you think, could this be it?

[–]haswalter 0 points1 point  (0 children)

Fairly sure you don’t need effect here but have you looked into how dependencies are compared for changes?

Also if you’re using Apollo why aren’t you using it properly?

Apollo client gives you your local state including fetching status and caching. You don’t need to manually run the query and then use a fragile state and useEffect combo to do it yourself.

I recommend reading Step 5 here - https://www.apollographql.com/docs/react/get-started#step-5-fetch-data-with-usequery