Back

Can i use Query.Search for infinity scroll pagination with react-query

  • 1
  • Databases
  • Web
  • Cloud
Examinor
29 Mar, 2024, 10:16

I want to get the searched result in pagination but i am not getting that so is it possible to paginate search results

TL;DR
Developers want to implement infinite scroll pagination with React Query using Query.Search. The issue seems to be with fetching the search results in pagination. The code provided shows the use of `useInfiniteQuery` and `authservice.getInfiniteUserResult` to fetch the data. The solution may involve debugging the `useGetInfinityUserSearch` and `getInfiniteUserResult` functions to ensure proper handling of pagination.
Examinor
29 Mar, 2024, 10:17
TypeScript
getInfiniteUserResult=async({pageParam,id}: { pageParam: number, id:string })=>{
  const queries: any[] = [Query.orderDesc("$updatedAt"), Query.limit(6),Query.search('userName', id)];

  if (pageParam) {
    queries.push(Query.cursorAfter(pageParam.toString()));
  }
  try {
    console.log(id);
    
    let posts = await this.database.listDocuments(
      conf.DATABASE_ID,
      conf.USER,
      queries
    );

    if (!posts) throw Error;
console.log(posts);

    return posts;
  } catch (error) {
    console.log(error);
  }
 }```
Examinor
29 Mar, 2024, 10:18

this is the function which gets the searched result

Examinor
29 Mar, 2024, 10:19
TypeScript
export const useGetInfinityUserSearch = (id:string) => {
    return useInfiniteQuery({
      queryKey: [QUERY_KEYS.GET_INFINITE_USER_SEARCH],
      queryFn: ({ pageParam }) =>{
        console.log(id);
        
       return authservice.getInfiniteUserResult({id:id, pageParam:pageParam})
      
      },
      getNextPageParam: (lastPage ) => {
       
      if (lastPage && lastPage.documents.length === 0) {
          return null;
      }
      const lastId = lastPage?.documents[lastPage.documents.length - 1].$id;
      console.log(lastPage);
      return lastId;
  
        
      },
    });
  };```

This is react-query
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more