#fetch next page

1 messages · Page 1 of 1 (latest)

mossy kestrel
#

https://tanstack.com/query/latest/docs/framework/react/guides/query-functions#query-function-variables

is there a way to pass the data that has been retrieved when fetchNextPage is being executed?

i wanted to use the document data (retrieved from the initial fetch) as my cursor instead of incementing or decrementing one by one.

A query function can be literally any function that returns a promise. The promise that is returned should either resolve the data or throw an error.

All of the following are valid query function configurations:

wild seal
#

I don't understand the question ..

mossy kestrel
# wild seal I don't understand the question ..

I'm working with useInfiniteQuery in React Query and want to leverage retrieved data as a cursor for infinite scroll pagination. This approach involves using the last item in the fetched data array to indicate the starting point for the next request, essentially requesting documents after the last one received. However, I'm encountering challenges in implementing this with React Query.

#

my function looks like this

const fetchThoughts = async (lastPost?: Timestamp): Promise<IThought[]> => {
  let q = query(
    thoughtsCollectionRef,
    orderBy('createdAt', 'desc'),
    limit(THOUGHTS_PER_PAGE)
  );

  if (lastPost) {
    q = query(q, startAfter(lastPost));
  }

  const querySnapshot = await getDocs(q);

  return querySnapshot.docs.map((doc) => ({
    ...(doc.data() as Omit<IThought, 'id'>),
    id: doc.id,
  }));
};