React Query

주요 Hook

useQuery

const { data, isLoading, isError } = useQuery("users", fetchUsers);

useMutation

특징

기능 설명
자동 캐싱 동일 쿼리는 최초 1회 요청 후 캐시 사용
자동 리페치 조건 만족 시 자동 재요청 (예: 창 포커스 시 최신 데이터)
실패 재시도 네트워크 오류 시 자동 재시도
오프라인 지원 오프라인 상태에서도 캐시 사용 가능, 온라인 복귀 시 재요청

npm install @tanstack/react-query@latest

1. 데이터 요청 함수 (fetchUsers)

const fetchUsers = async () => {
  const response = await axios.get("http://localhost:8080/ch09/user2");
  return response.data;
};

2. 삭제 요청 함수 (deleteUser)

const deleteUser = async (uid) => {
  const response = await axios.delete(
    `http://localhost:8080/ch09/user2/${uid}`
  );
  return response;
};