Skip to content

Commit 0cc904a

Browse files
authored
Merge pull request #318 from prgrms-web-devcourse-final-project/refactor/queryclient-singleton
[refacto]: QueryClient를 전역 싱글톤 인스턴스로 변경
2 parents b0b6e32 + b22f97a commit 0cc904a

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

src/apis/axios.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import axios from 'axios';
22
import { useAuthStore } from '@/store/authStore';
33
import { reissueToken } from '@/apis/auth';
4-
import { QueryClient } from '@tanstack/react-query';
5-
6-
const queryClient = new QueryClient();
4+
import { queryClient } from '@/utils';
75

86
const API_BASE_URL =
97
import.meta.env.MODE === 'development'
@@ -71,8 +69,7 @@ axiosInstance.interceptors.response.use(
7169
} catch (error) {
7270
useAuthStore.getState().logout();
7371
useAuthStore.persist.clearStorage();
74-
queryClient.removeQueries({ queryKey: ['myProfile'] }); // 프로필 정보 캐시 초기화
75-
queryClient.removeQueries({ queryKey: ['userPosts', 'me'] }); // 포스트 정보 캐시 초기화
72+
queryClient.clear(); // 모든 쿼리, 변이 상태, 캐시 전부 제거
7673
console.error('AT 토큰 재발급 실패:', error);
7774

7875
return Promise.reject(error);

src/main.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { createRoot } from 'react-dom/client';
22
import { BrowserRouter } from 'react-router';
3-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
3+
import { QueryClientProvider } from '@tanstack/react-query';
44
import { registerSW } from 'virtual:pwa-register';
55
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
66
import './styles/index.css';
77
import App from './App.tsx';
88
import ScrollToTop from '@/components/ScrollToTop.tsx';
9+
import { queryClient } from '@/utils';
910

1011
// PWA 서비스 워커 등록
1112
const updateSW = registerSW({
@@ -19,9 +20,6 @@ const updateSW = registerSW({
1920
},
2021
});
2122

22-
// 🔹 QueryClient 생성
23-
const queryClient = new QueryClient();
24-
2523
createRoot(document.getElementById('root')!).render(
2624
<QueryClientProvider client={queryClient}>
2725
<BrowserRouter>

src/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { queryClient } from './queryClient';
12
import { cn } from './cn';
23

3-
export { cn };
4+
export { cn, queryClient };

src/utils/queryClient.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { QueryClient } from '@tanstack/react-query';
2+
3+
export const queryClient = new QueryClient();

0 commit comments

Comments
 (0)