2025. 5. 8. 00:25ใFront-end/Tools

๋ชฉ์ฐจ
1. TanStack Query๋
2. ์ค์น ๋ฐ ๊ธฐ๋ณธ ์ค์
2-1. TanStack ํจํค์ง ์ค์น
2-2. Query Client ์์ฑ ๋ฐ Provider ์ค์
3. ๋ฐ์ดํฐ ์กฐํ (Fetching)
4. ๋ฐ์ดํฐ ๋ณ๊ฒฝ (Mutations)
5. ์ฟผ๋ฆฌ ๋ฌดํจํ ๋ฐ ๋ฆฌํจ์นญ
6. ๊ธฐํ ๊ธฐ๋ฅ๋ค
7. ํด
1. TanStack Query๋
์๋ฒ๋ก๋ถํฐ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ, ์บ์ฑ, ์ ์ด ๋ฑ ๋ฐ์ดํฐ์ ํจ์จ์ ์ธ ๊ด๋ฆฌ๋ฅผ ๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
https://tanstack.com/query/latest
TanStack Query
Powerful asynchronous state management, server-state utilities and data fetching. Fetch, cache, update, and wrangle all forms of async data in your TS/JS, React, Vue, Solid, Svelte & Angular applications all without touching any "global state"
tanstack.com
๋ํ์ ์ธ ๊ธฐ๋ฅ
- ๋ฐ์ดํฐ ํจ์นญ ๋ฐ ์บ์ฑ : API๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์บ์์ ์ ์ฅํ์ฌ, ๋ถํ์ํ ๋คํธ์ํฌ ์์ฒญ ๋ฐฉ์ง
- ์๋ ๋ฆฌํจ์นญ : ๋ฐ์ดํฐ์ ์ ์ ๋๋ฅผ ์ ์งํ๊ธฐ ์ํด, ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์๋์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐฑ์
- ๋ฌดํ ์คํฌ๋กค, ํ์ด์ง๋ค์ด์ ๋ฑ์ ์ฑ๋ฅ ์ต์ ํ
- ์๋ฌ ์ฒ๋ฆฌ ๋ฐ ์ฌ์๋ : ๋คํธ์ํฌ ์ค๋ฅ ๋ฐ์ ์ ์๋์ผ๋ก ์ฌ์๋, ์๋ฌ ์ํ๋ฅผ ์ฝ๊ฒ ๊ด๋ฆฌ
- ์ฟผ๋ฆฌ ๋ฌดํจํ : ๋ฐ์ดํฐ ๋ณ๊ฒฝ ํ ๊ด๋ จ๋ ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํจํํ์ฌ ์ต์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์
2. ์ค์น ๋ฐ ๊ธฐ๋ณธ ์ค์
1. Tanstack Query ํจํค์ง ์ค์น
npm i @tanstack/react-query
2. QueryClient ์์ฑ ๋ฐ Provider ์ค์
TanStack Query๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด QueryClient๋ฅผ ์์ฑํ๊ณ , ๋ฃจํธ์ QueryClientProvider๋ฅผ ์ค์ ํ๋ค.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
function App() {
return (
<QueryClientProvider client={queryClient}>
{/* application components */}
</QueryClientProvider>
);
}
3. ๋ฐ์ดํฐ ์กฐํ (Fetching)
useQuery hook์ ์ฌ์ฉํ์ฌ fetchingํ๋ค.
import { useQuery } from '@tanstack/react-query';
function Posts() {
const { data, isLoading, error } = useQuery({
queryKey: ['posts'],
queryFn: async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
},
});
if (isLoading) retun <div> loading... </div>
if (error) return <div> error:{error.msg}</div>
return (
<ul>
{data.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
}
** queryKey : ์บ์ ์๋ณ์
** queryFn : ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๋น๋๊ธฐ ํจ์
4. ๋ฐ์ดํฐ ๋ณ๊ฒฝ (Mutations)
useMutation hook์ ์ฌ์ฉํ์ฌ ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์์ ํ๋ค.
import {useMutation, useQueryClient } from '@tanstack/reac-query';
function AddPost() {
const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: async (newPost) => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts',
method: 'POST',
body: JSON.stringify(newPost),
headers: { 'Content-Type': 'application/json'},
});
if(!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
},
onSuccess: () => {
// 'posts' query๋ฅผ ๋ฌดํจํํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์๋ก ๊ณ ์นจ
queryClient.invalidateQueries({ queryKey: ['posts']});
},
});
const handleAddPost = () => {
mutation.mutate({ title: 'new post', body: 'contents', userId: 1 });
};
return <button onClick={handleAddPost}> add post</button>;
}
onSuccess ์ฝ๋ฐฑ์์ ๊ด๋ จ ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํจํํ์ฌ ์ต์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
5. ์ฟผ๋ฆฌ ๋ฌดํจํ ๋ฐ ๋ฆฌํจ์นญ
๋ฐ์ดํฐ ๋ณ๊ฒฝ ํ, ํน์ ํน์ ์กฐ๊ฑด์ ๋ฐ๋ผ ํน์ ์ฟผ๋ฆฌ๋ฅผ ๋ฌดํจํํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์๋ก ๊ณ ์นจํ ์ ์๋ค.
queryClient.invalidateQueries({queryKey: ['posts'] });
6. ๊ธฐํ ๊ธฐ๋ฅ๋ค
enabled ์ต์ ์ ์ฌ์ฉํด์ ์ฟผ๋ฆฌ ์คํ ์ฌ๋ถ(ํ์ฑํ ์กฐ๊ฑด)์ ์ ์ดํ ์ ์๋ค.
useQuery({
queryKey: ['user', userId],
queryFn: fetchUser,
enabled: !!userId, //userId๊ฐ ์์ ๋๋ง ์คํ
});
select ์ต์ ์ ์ฌ์ฉํด์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ ํํ๊ณ ๋ณํํ ์ ์๋ค.
useQuery({
queryKey: ['posts'],
queryFn: fetchPosts,
select: (data) => data.slice(0, 10), //์์ 10๊ฐ ํฌ์คํธ ์ ํ
});
staleTime, cacheTime ์ต์ ์ ์ฌ์ฉํด์ ๋ฐ์ดํฐ์ ์ ์ ๋์ ์บ์ ์ ์ง ์๊ฐ์ ์ค์ ํ ์ ์๋ค.
useQuery({
queryKey: ['posts'],
queryFn: fetchPosts,
staleTime: 1000 * 60 * 5, //5๋ถ ๋์ freshํ ๋ฐ์ดํฐ๋ก ๊ฐ์ฃผ
cacheTime: 1000 * 60 * 10, //10๋ถ ๋์ ์บ์ ์ ์ง
});
7. ํด
React Query Devtools๋ฅผ ์ฌ์ฉํด์ ์ฟผ๋ฆฌ ์ํ๋ฅผ ์๊ฐ์ ์ผ๋ก ํ์ธํ ์ ์๋ค.
npm install @tanstack/react-query-devtools
import { ReactQueryDevtools } from '@tanstack/reac-query-devtools';
function App() {
return (
<QueryClientProvider client={queryClient}>
{/* application components */}
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}
์์ ์ค์ต
https://ldd6cr-adness.tistory.com/304
TanStack Query ์ฌ์ฉํด๋ณด๊ธฐ
์ง๋ ๋ฒ์ TanStack Query์ ๋ํด ์ญ ํ์ด๋ดค์ผ๋JSONPlaceholder๋ฅผ ์ฌ์ฉํด์ ๋ฏธ๋ ํ๋ก์ ํธ ์์ ๋ก Tanstack Query๋ฅผ ํ์ฉํด๋ณด์. ์ค์ต ํ๋ก์ ํธ ๋ด์ฉ๊ฒ์ํ ๋ทฐ์ด : ๊ฒ์๊ธ ๋ชฉ๋ก์ ๋ถ๋ฌ์ค๊ณ , ์์ธ ๋ด์ฉ์ ํ
ldd6cr-adness.tistory.com
'Front-end > Tools' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Zustand ์ฌ์ฉํด๋ณด๊ธฐ (0) | 2025.05.15 |
|---|---|
| TanStack Query ์ฌ์ฉํด๋ณด๊ธฐ (0) | 2025.05.14 |
| Zustand๋ ๋ญ๊น (0) | 2025.04.04 |
| ํ๋ก ํธ์๋ ๊ฐ๋ฐ ๋๊ตฌ ๋ชจ์ (0) | 2025.03.18 |
| Font Awesome ์์ด์ฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (0) | 2025.02.15 |
GitHub