๐Ÿ–ฅFrontEnd/React

tanstack query, useInfiniteQuery์™€ useQuery์™€์˜ ์ฐจ์ด์ 

hellohailie 2025. 1. 3. 00:55
๋ฐ˜์‘ํ˜•

 

useQuery์™€์˜ ์ฐจ์ด์ 

1. ๋ฐ˜ํ™˜ ๊ฐ์ฒด์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐ์ดํ„ฐ ์†์„ฑ์˜ ๊ตฌ์กฐ์™€ ํ˜•ํƒœ (pages, pageParams)

useQuery๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฟผ๋ฆฌ ํ•จ์ˆ˜์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ data ์†์„ฑ์— ๋‹ด๊ฒจ ๋ฐ˜ํ™˜๋˜์ง€๋งŒ, useInfiniteQuery ์—์„œ๋Š” ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด๊ฐ€ data์™€ pages๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ์†์„ฑ์„ ํฌํ•จํ•œ๋‹ค. pages๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ํŽ˜์ด์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด์˜ ๋ฐฐ์—ด์ด๋‹ค. ๋”ฐ๋ผ์„œ pages ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋Š” useQuery๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๊ฐ๊ฐ์˜ ์ฟผ๋ฆฌ์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ํ•ด๋‹นํ•œ๋‹ค. 

 

 

pageParams (ํŽ˜์ด์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜)๋„ ์žˆ๋Š”๋ฐ, ์ด๋Š” ๊ฐ ํŽ˜์ด์ง€๋งˆ๋‚˜ ์‚ฌ์šฉํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค. (์ž˜ ์‚ฌ์šฉ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค.)

 

 

๊ทธ๋ž˜์„œ ๊ฐ ์ฟผ๋ฆฌ๋Š” pages ๋ฐฐ์—ด์˜ ์ž์‹ ๋งŒ์˜ ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ทธ ์š”์†Œ๋Š” ํ•ด๋‹น ์ฟผ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. 

์ฟผ๋ฆฌ๋Š” ํŽ˜์ด์ง€๋ฅผ ์ง„ํ–‰ํ•จ์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ๋˜๊ณ , ํŽ˜์ด์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ฒ€์ƒ‰๋œ ์ฟผ๋ฆฌ์˜ ํ‚ค๋ฅผ ์ถ”์ ํ•œ๋‹ค. 

 

 

2. syntax

useInfiniteQuery ์˜ต์…˜ ์ค‘ getNextPageParam: (lastPage, allPages) => ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€ ๋ฐ์ดํ„ฐ๋‚˜ ๋ชจ๋“  ํŽ˜์ด์ง€์˜ ๋ฐ์ดํ„ฐ์—์„œ ๋‹ค์Œ ํŽ˜์ด์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜

 

 

* useInfiniteQuery์˜ return object properties

fetchNextPage => ์‚ฌ์šฉ์ž๊ฐ€ ๋” ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ์€ ํ•จ์ˆ˜

hasNextPage => ๋‹ค์Œ ํŽ˜์ด์ง€๊ฐ€ ์žˆ๋Š”์ง€ 

isFetchingNextPage => ๋‹ค์Œ ํŽ˜์ด์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘์ธ์ง€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘์ธ์ง€๋ฅผ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์ฐธ๊ณ )

https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery

 

useInfiniteQuery | TanStack Query React Docs

tsx const { fetchNextPage, fetchPreviousPage, hasNextPage, hasPreviousPage, isFetchingNextPage, isFetchingPreviousPage, promise, ...result } = useInfiniteQuery({ queryKey, queryFn: ({ pageParam }) = f...

tanstack.com

 

 

import InfiniteScroll from "react-infinite-scroller";
import { Species } from "./Species";
import { useInfiniteQuery } from "@tanstack/react-query";

const initialUrl = "https://swapi...";
const fetchUrl = async (url) => {
  const response = await fetch(url);
  return response.json();
};

export function InfiniteSpecies() {
  const {
    data,
    fetchNextPage,
    hasNextPage,
    isLoading,
    isError,
    error,
    isFetching,
  } = useInfiniteQuery({
    queryKey: ["sw-species"],
    queryFn: ({ pageParam = initialUrl }) => fetchUrl(pageParam),
    getNextPageParam: (lastPage) => lastPage.next || undefined,
  });

  if (isLoading) return <div className='loading'>Loading...</div>;
  if (isError) return <div>{error.toString()}</div>;

  return (
    <>
      {isFetching && <div className='loading'>Loading...</div>}
      <InfiniteScroll
        pageStart={0}
        loadMore={() => {
          if (!isFetching) {
            fetchNextPage();
          }
        }}
        hasMore={hasNextPage}
        loader={
          <div className='loader' key={0}>
            Loading ...
          </div>
        }
      >
        {data.pages.map((page, index) => (
          <ul key={index}>
            {page.results.map((species) => (
              <Species
                key={species.name}
                name={species.name}
                language={species.language}
                averageLifespan={species.average_lifespan}
              />
            ))}
          </ul>
        ))}
      </InfiniteScroll>
    </>
  );
}
๋ฐ˜์‘ํ˜•