-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using a setState inside a startTransition with a fetchMore cause Apollo to clear cached results when using a policy #11642
Comments
Hey @Arno500 👋 I'll dig into this a bit more deeply, but as an FYI, something that stands out immediately is that React doesn't support async functions inside
Its possible something is happening here that gets React in a weird state. Transitions essentially render your component in 2 different states at the "same time", one with the new value, and one with the old value. Its possible the async function might be doing something weird here and confusing React. I'll play around with your demo a bit to understand if this is the root issue, but just wanted you to know that React doesn't support async functions for |
Thank you very much for that thorough explanation. Then how would you recommend reacting on only the new values? |
It would mostly be a matter of moving some code around. Here is a version that uses a synchronous callback, but allows you to wait for the data to finish: const fetchMoreData = async () => {
console.log('Probably have more data: ', hasMore);
if (!hasMore) return;
let promise!: Promise<ApolloQueryResult<any>>;
startTransition(() => {
promise = fetchMore({
variables: {
limit: 5,
offset: data.histories.length,
},
});
});
const { data: newData } = await promise;
console.log('Just received data: ', newData);
if (newData.histories.length < 1) {
console.log('Out of data, we consumed everything');
setHasMore(false);
}
}; I threw this into my own fork of the reproduction, but looks like it doesn't really change much. So it seems like its likely an Apollo Client bug! |
I can reproduce this issue in my code as well. I used this workaround of using readyQuery to solve temporarily this issue : |
@DenisLaboureyras can you please verify that you are still experiencing this in 3.9.6? |
Some discovery notes: I think I've narrowed this down to something in |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Issue Description
Hello,
I noticed that whenever I set a state based on the new data received, IF AND ONLY IF the incoming array is empty (???), it resets the results to an empty array for some reason
Link to Reproduction
https://stackblitz.com/edit/stackblitz-starters-hswnfx?file=src%2Fcomponents%2FPaginatedQuery.tsx
Reproduction Steps
fetchMore
inside astartTransition
fetchMore
until you get a perfectly empty array, signifying that you got to the end of the resultsfetchMore
(here we use ahasMore
, similar to our own code base)data
being 'reset'@apollo/client
version3.9.5
The text was updated successfully, but these errors were encountered: