Open
Description
openedon Nov 7, 2022
Due to the fact that we have to use merge
+ serializeQueryArgs
and ignore some pagination params, usePrefetch can't be used to prefetch data based on this ignored params.
As a result I see few unwanted side effects when use usePrefetch
+ useQuery
:
- prefetched data get passed though
merge
method and became available for users - useQuery with initial params may not even be executed at all.. and looks like there is race condition (some time only one request with prefetch params, some times 2 expected requests - regular + prefetch)
Code example:
api.usePrefetch('getData')({ ...query, page: query.page + 1 });
const { data } = api.useGetData(query);
....
endpoints: build => ({
getData: build.query<Item[], Query>({
query: data => ({ url: '/find', data, method: 'post' }),
serializeQueryArgs({ endpointName, queryArgs, endpointDefinition }) {
return defaultSerializeQueryArgs({
queryArgs: omit(queryArgs, 'page'),
endpointName,
endpointDefinition,
});
},
merge(currentCacheData, responseData) {
currentCacheData.push(...responseData);
},
forceRefetch({ currentArg, previousArg }) {
return currentArg.page !== previousArg?.page;
},
keepUnusedDataFor: 0,
}),
});
Expected Behaviour
- usePrefetch data should not be processed by
merge
- usePrefetch should not impact useQuery behaviour
- prefetched data should be applied in
merge
only after it was requested through useQuery
Use Case
Why I might need usePrefetch for pagination?
We want to load portion of data to show it immediately and prefetch next portion to show latter on ASAP (next page / scroll event, etc).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment