TanStack Query (구 React Query) 는 React 애플리케이션에서 서버 상태를 효율적으로 관리하기 위한 라이브러리이다. 서버 데이터의 fetching, caching, synchronizing, 그리고 업데이트 로직을 간편하게 구현할 수 있다.
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
const fetchPokemon = async () => {
const { data } = await axios.get('https://pokeapi.co/api/v2/pokemon');
return data;
};
const PokemonList = () => {
const { data, isLoading, error } = useQuery(['pokemon'], fetchPokemon);
if (isLoading) return <p>Loading...</p>;
if (error) return <p>Error: {error.message}</p>;
return (
<ul>
{data.results.map((pokemon) => (
<li key={pokemon.name}>{pokemon.name}</li>
))}
</ul>
);
};
TanStack Query는 서버 데이터 상태 관리와 관련된 문제를 쉽게 해결할 수 있는 강력한 도구이다. 특히, 복잡한 API 호출과 실시간 동기화가 필요한 프로젝트에서 생산성을 크게 향상시킬 수 있다.