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

RFC: Request to add useInfiniteQuery hook just like one on react-query #3726

Open
schang124 opened this issue Jan 1, 2025 · 0 comments
Open
Labels
future 🔮 An enhancement or feature proposal that will be addressed after the next release

Comments

@schang124
Copy link

schang124 commented Jan 1, 2025

Summary

  • The infinite scroll example on the doucment is one of great way to apply infinte query, but if you can provide such api useInfiniteQuery on react-query that would be really convenient!
  • The advantage for this hook would be:
    • Do not need page control with useState and it is included with the hook.
    • Supports both previous and next pagination.
    • Much convenient access and manipulation to list data, if needed.

Proposed Solution

import React from 'react';
import { useInfiniteQuery, gql } from 'urql';

const PageQuery = gql`
  query Page($first: Int!, $after: String) {
    todos(first: $first, after: $after) {
      nodes {
        id
        name
      }
      pageInfo {
        hasPrevPage
        hasNextPage
        prevCursor
        nextCursor
      }
    }
  }
`;

const SearchResultPage = ({ variables, isLastPage, isFirstPage, onLoadMore }) => {
  const [{ 
    data, 
    fetching, 
    fetchNextPage,
    fetchPreviousPage, 
    error 
  }] = useInfiniteQuery({ 
    query: PageQuery, 
    variables ,
    initialPageParam: 1,
    ...options,
    getPreviousPageParam: (firstPage, allPages, firstPageParam, allPageParams) =>
      firstPage.prevCursor,
    getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
      lastPage.nextCursor,
  });
  
  const todos = data?.pages.map((page) => page.todos).flat();

  return (
    <div>
      {error && <p>Oh no... {error.message}</p>}x
      {fetching && <p>Loading...</p>}
      {todos && (
        <>
          {isFirstPage && todos.pageInfo.hasPrevPage && (
            <button onClick={fetchPreviousPage}>load prev</button>
          )}
          {todos.nodes.map(todo => (
            <div key={todo.id}>
              {todo.id}: {todo.name}
            </div>
          ))}
          {isLastPage && todos.pageInfo.hasNextPage && (
            <button onClick={fetchNextPage}>load next</button>
          )}
        </>
      )}
    </div>
  );
};

Requirements

@schang124 schang124 added the future 🔮 An enhancement or feature proposal that will be addressed after the next release label Jan 1, 2025
@schang124 schang124 changed the title RFC: Does your tem have any plan to develop useInfiniteQuery just like react-query? RFC: Request to add useInfiniteQuery hook just like react-query Jan 1, 2025
@schang124 schang124 changed the title RFC: Request to add useInfiniteQuery hook just like react-query RFC: Request to add useInfiniteQuery hook just like one on react-query Jan 1, 2025
@schang124 schang124 changed the title RFC: Request to add useInfiniteQuery hook just like one on react-query RFC: Request to add useInfiniteQuery hook just like one on react-query Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
future 🔮 An enhancement or feature proposal that will be addressed after the next release
Projects
None yet
Development

No branches or pull requests

1 participant