Ensure refetch
, fetchMore
, and subscribeToMore
functions returned by useSuspenseQuery
are referentially stable between renders
#10656
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the
refetch
,fetchMore
, andsubscribeToMore
functions exported byuseSuspenseQuery
are recreated anytime data has changed, causing a rerender. If you were using these functions inuseEffect
, for example, this could cause that effect to run each timedata
was updated.Here is an example taken from the Spotify showcase that demonstrates loading playback state and using a subscription to subscribe to updates to it (with slight variation):
The subscription is fired each time playback state changes on the server, for example the current track's progress. This can happen as often as once a second. Because
subscribeToMore
is recreated anytimedata
changes, this meant the effect would re-run every second, constantly toggling between unsubscribing from the subscription and resubscribing.These functions only rely on the
observable
as part of its implementation, therefore the recreation of the function was unnecessary. This PR ensures these functions are only recreated anytime theobservable
changes to ensure they remain referentially stable between renders, even whendata
is updated.Checklist: