apollo-client: Fetch more function is undefined for useLazyQuery react-apollo hook
I use react-apollo useLazyQuery hook. My goal to use it in fetch more button click. However, when I try to use it the fetchMore function is undefined.
Here the call to it:
const [getMyData,{ loading, data, fetchMore }] = useLazyQuery(
myQuery,
{
notifyOnNetworkStatusChange: true,
fetchPolicy: 'network-only',
variables: {}
})
Even if I run lazy on component ready, fetchMoreStill undefined. The question is why it is undefined?
“apollo-client”: “^2.6.4”, “react-apollo”: “^3.1.3”,
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 21 (2 by maintainers)
I was able to solve mine by checking if fetchMore is undefined like: if (fetchMore === undefined) return; fetchMore({});
I ran into this today and the
fetchMore
that is undefined seems to be internal to the hook.It appears to be undefined before the query is fired (this makes sense to me) and when
loading
is true.Checking
loading
,called
, and the returned data before callingfetchMore
seemed to fix my issueIn 3.5.0 (which is in RC and which you can download
@apollo/client@beta
), the methods will always be defined!This should now be resolved in
@apollo/client@3.5.0
. Let us know if you notice any issues. Thanks!Upgraded to
3.4.16
and can confirm that checking thatfetchMore
isn’tundefined
before calling seems to do the trick.Would be great if the docs actually clarified that
fetchMore
can be undefined (https://www.apollographql.com/docs/react/api/react/hooks/#fetchmore. Note this links to the docs onuseQuery
, even though I tapped thefetchMore
underuseLazyQuery
). And if there was some notes on the relationship betweencalled
andfetchMore
.IE, I was writing
(called && fetchMore ? fetchMore : runQuery)({ variables: ... })
, but the more simple(fetchMore ?? runQuery)({ variables: ... })
seems to have the exact same effect.Lovely, @sashberd . My workaround was to refactor all useLazyQuery-related code into a custom React hook. For some reason, doing all the fetchMore work and new calls in a separate hook works whereas doing it within the same component doesn’t. May be an interesting avenue to explore.