Hello,
I’m using Apollo Client to paginate a feed of data.
My local state contains two tabs that are supposed to fetch either active data or draft data.
I’ve created a query that takes the active status to fetch the data from.
When the query first loads (when the component mounts), it works very well and I manage to paginate the data with fetchMore
.
However, when I click on the second tab, the query is not rerun and the cache is not invalidated.
I’ve managed to make it work somehow by using refetch
when the local state change, but I’d like to know if there is a better/cleaner way.
I’d have expected the query to rerun on its own as the state variable had changed but it does not fire a new request and it fetches the data from the cache even though the variable has change.
Here is the code:
// This works perfectly fine.
const { loading, error, data, fetchMore, refetch } =
useQuery(FEATURED_VIDEOS, {
variables: {
filters: {
limit: 12,
active: activeTab === "featured"
offset: 0,
},
},
});
// This is called to change the active tab by updating React state
const changeTab = (newTab: string) => {
setSelected(null);
setActiveTab(newTab);
refetch({
filters: {
limit: 12,
offset: data?.featuredVideos.length,
active: newTab === "featured",
},
});
};
Thanks for your help,