Skip to content
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

RTK-Query: add ability to get access to latest batch info when using merge #2870

Closed
ivan-jelev opened this issue Nov 6, 2022 · 4 comments
Closed

Comments

@ivan-jelev
Copy link

ivan-jelev commented Nov 6, 2022

merge prop introduced in v1.9.0 is super great and makes infinity scroll almost out-of-the-box experience!

However merge just give ability to accumulate data, but in some cases we need a bit more - we need to know what's inside the latest batch in order to consider if we should keep loading or not.

Use Case

Infinite loading over unknown amount of pages with non guaranteed items count per page. For example we would like to set skip: true for a query when we know that latest batch had exactly 0 items.

Right now we can't achieve this as data in data & currentData props is accumulated data.

Proposal

The proposal is to repurpose currentData for this case not to contain accumulated data, but only state of latest/current batch.
Or just introduce another prop which would hold this data (latestData/latestBatch or whatever you consider reasonable).

Example of what I would like to achieve:

const [hasMoreData, setHasMoreDataState] = useState(true);
const { data = [], latestBatch = [] } = api.useQuery(..., { skip: !hasMoreData });

useEffect(() => {
  const mightHaveMore = latestBatch.length > 0;
  setHasMoreDataState(mightHaveMore);
}, [latestBatch.length]);

Thank you!

@ivan-jelev
Copy link
Author

Additionally I've found potential issue.. when we have some accumulated data and then at some point skip prop changes to true we are losing all the data.. data & currentData are empty when {skip: true}. My expectation would be to preserve the data when skip is changed.

@markerikson
Copy link
Collaborator

Filed #2871 to cover the data aspect, since that's a completely separate issue.

For this question, no, I don't think we would want to change currentData. Its purpose is to keep the actual accumulated data when a different query is started.

Not sure what an actual API solution would look like, but as a workaround, you could use transformResponse / merge to keep track of, say, {items: Item[], mostRecentItems: number} or something like that.

@ivan-jelev
Copy link
Author

Yep.. it's achievable through transformResponse + merge to keep track latest batch and makes more sense to deal with on data level instead of API level.

Thanks for the idea!

@ser-emejia
Copy link

Yep.. it's achievable through transformResponse + merge to keep track latest batch and makes more sense to deal with on data level instead of API level.

Thanks for the idea!

Hello, could you share your implementation, please 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants