useSubscription "loading" and "data" out of sync when used with other hooks
See original GitHub issueI am using version 3.2.4 of @apollo/client and there is no way of using a useEffect to check when loading moves from true to false with new data when you have other hooks changing state.
You simply get loading
back to false
and the old data as soon as you are using other hooks that changes the state of your component.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:7
Top Results From Across the Web
Apollo client useSubscription hook is loading forever. How do ...
But my useSubscription hook has the status of loading=true forever, and no data is being displayed. I am not sure where to start ......
Read more >GraphQL subscriptions with Apollo Client React Hooks and ...
Again, I'm using a functional React component and a couple of different React hooks. The first hook is useSubscription on line 7.
Read more >Use GraphQL Subscriptions in a React App - Pluralsight
Instead of using the useQuery hook, you used the useSubscription hook. The hook still returns loading , data , and error in the ......
Read more >Need to use setstate() of data received from GraphQL ...
setState({ fishes: data }); }. And Change const haveAnyFish = () => ( <Subscription subscription={gql`${SUBSCRIPTION_SYNC}`} > {({ loading, ...
Read more >RTK Query Overview - Redux Toolkit
It is designed to simplify common cases for loading data in a web ... like Redux to cache data, the use cases are...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We are running into this same bug. As far as I can tell this problem is caused because the
useSubscription
hook stores the most recent data returned by the subscription in state, but it doesn’t store or update that state when the variables change. In that case a render is triggered and the hook returns{ loading: true, data: undefined }
, but still has the previous data in state. If another render is triggered after that by anything other than data being returned from the subscription, then the old datauseSubscription
still has in state will be returned.I think the right way to fix this would be for
useSubscription
to also store theloading: true
state change in state, or at least update thedata
stored in state toundefined
when loading starts, so that after a variable change the hook would returndata: undefined
until some new data actually came through the subscription.I tried making this change to
useSubscription.ts
but it broke a lot of unit tests so I’m not super confident about it. In any event it would be a bigger than 20 line change so I think even if it is the correct fix, it would qualify as a Big PR.Would love to get some feedback from someone more familiar with the
useSubscription
and theSubscriptionData
class. Maybe @hwillson?We finally found a work around for this issue.
We don’t use the returned values of the
useSubscription
hook anymore but manually maintain a state containingloading
anddata
. We have a usEffect hook, that sets theloading
state totrue
when subscription variable changes and we set it back tofalse
with the newdata
in theonSubscriptionData
that way we can be sure that whenloading
moves fromtrue
tofalse
we have the new data with it.Hope this will help someone and that this will be fixed soon in the
useSubscription
hook. I had a look the hook trying to figure out why it was happening but couldn’t find a solution.