Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

new_bird-hyun

React Query - 복잡한 서버 상태 핸들링 본문

코딩 공부

React Query - 복잡한 서버 상태 핸들링

새혀니 2025. 4. 9. 14:52

npm install @tanstack/react-query

import { useQuery, QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

function Posts() {
  const { data, isLoading, error } = useQuery(['posts'], () =>
    fetch('/api/posts').then(res => res.json())
  );

  if (isLoading) return <p>불러오는 중...</p>;
  if (error) return <p>에러!</p>;

  return (
    <ul>
      {data.map((post: any) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  );
}

export default function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <Posts />
    </QueryClientProvider>
  );
}