-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⚙️ Chore: Tanstack Query 업그레이드 #96
Changes from all commits
b19d325
0b8c55b
dfa5aa8
d5fb3f1
ce8ab0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
import { useQuery } from 'react-query'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
import { getDashboardsList } from '@/services/getService'; | ||
import { setDashboards } from '@/store/reducers/dashboardsSlice'; | ||
|
||
const fetchDashboards = async () => { | ||
const response = await getDashboardsList(); | ||
|
||
if (response.status !== 200) { | ||
throw new Error('Failed to fetch dashboards'); | ||
} | ||
|
||
return response.data; | ||
}; | ||
|
||
export const useFetchDashboards = () => { | ||
const dispatch = useDispatch(); | ||
|
||
return useQuery('dashboards', () => fetchDashboards(), { | ||
onSuccess: (data) => { | ||
dispatch(setDashboards({ dashboards: data.dashboards, totalCount: data.totalCount })); | ||
const { isLoading, error, data, isFetching } = useQuery({ | ||
queryKey: ['dashboards'], // 쿼리 키는 문자열이나 배열로 지정할 수 있습니다 | ||
queryFn: async () => { | ||
try { | ||
const response = await getDashboardsList(); | ||
if (response.status !== 200) { | ||
throw new Error('Failed to fetch dashboards'); | ||
} | ||
const data = response.data; | ||
dispatch(setDashboards({ dashboards: data.dashboards, totalCount: data.totalCount })); | ||
return data; // 데이터를 반환해야 합니다 | ||
} catch (error) { | ||
// 에러 처리 | ||
throw new Error('데이터를 불러오는 중 에러 발생: ' + error); | ||
} | ||
}, | ||
}); | ||
|
||
return { isLoading, error, data, isFetching }; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import '@/styles/globals.css'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; | ||
import type { AppProps } from 'next/app'; | ||
import { QueryClient, QueryClientProvider } from 'react-query'; | ||
import { Provider } from 'react-redux'; | ||
import { PersistGate } from 'redux-persist/integration/react'; | ||
|
||
|
@@ -17,6 +18,7 @@ export default function App({ Component, pageProps }: AppProps) { | |
<MainLayout> | ||
<Component {...pageProps} /> | ||
</MainLayout> | ||
<ReactQueryDevtools initialIsOpen={false} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개발 모드에서 리액트 쿼리 개발자 툴을 보실 수 있습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개발 모드는 run dev 했을때인가요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 맞습니다! |
||
</QueryClientProvider> | ||
</PersistGate> | ||
</Provider> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
원래 useFetchData랑 똑같이 사용하면 되나요???
const { data, isLoading, error } = useFetchData...
에러 핸들링 처리를 useQuery 가 해주는 건가요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵 뭐 바꿀필요 없이 원래 쓰시던 대로 쓰시면 됩니다!
내부 로직만 바뀌었고, 파라미터나 리턴값으로 리스폰스 데이터 주는 것은 똑같아요!