react-query ์ฌ์ฉ์ด์
0. ์๋ฒ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ ๋ ์ด์ด ์ ์ฒด๋ฅผ ์ถ์ํ, ๊ฐ๋ฐ์๊ฐ ๊ด๋ฆฌํ๋ ์ฑ ๋ด์ ์ํ์์ UI ์ํ์๋ง ์ง์ค
1. ๋ฐ์ดํฐ๋ฅผ ์๋์ผ๋ก ์บ์ํ๊ณ , ์์ฒญ์ ์ต์ ํํ ์ ์์ต๋๋ค.
2. ๋คํธ์ํฌ ์ค๋ฅ ๋ฐ ์์ธ ์ํฉ์ ์ฒ๋ฆฌํ๊ณ ์๋์ผ๋ก ์ฌ์๋ ํ ์ ์๋ ๋ฉ์ปค๋์ฆ์ ์ ๊ณต
3. ๊ธฐ์กด ๋ฏธ๋ค์จ์ด๋ก ๋น๋๊ธฐ ํต์ ์ ์ฒ๋ฆฌํ์๋, api ๋์๋ง๋ค ์ํ์ ์๋ต๊ฐ์ ์ ์ญ์ผ๋ก ๊ด๋ฆฌํ๋๋ฐ
์ด๋ ๋นํจ์จ์ ์ -> react-query๋ react ์ปดํฌ๋ํธ์ ํตํฉํด ๊ฐํธํ๊ฒ ์ํ๋ฅผ ๊ด๋ฆฌ
react-query์ ์๋ฒ ๋ฐ์ดํฐ ์ํ ๋ถ๋ฅ
|
Store๋ ์ ์ญ์ํ๊ฐ ์ ์ฅ, ๊ด๋ฆฌํ๋ ๊ณต๊ฐ
๋น๋๊ธฐ ํต์ ์์ฒด์ ์ด์ ์ฝํ์๋ ๋ค์ํ ์ ์ฑ ๋ค๊ณผ ํจ๊ป Store์ ์ญํ ์ด ๋น๋ํด์ง.
๊ธฐ์กด redux์์๋ redux-saga, redux-thunk ๋ฏธ๋ค์จ์ด๋ก ๋น๋๊ธฐ ํต์ ์ ์ฒ๋ฆฌํ๋ค.
ํ ํ๋ก๋ํธ์ api ๋์๋ง๋ค ajax ์ํ์ ์๋ต๊ฐ์ ๋ฐ๋ผ๋ณด๋ ์ํ๊ฐ ์๊น(success, failure ๋ฑ)
์ด๊ฒ๊น์ง ์ ์ญ ์ํ๋ก ๊ด๋ฆฌํ๋ ๊ฑด ๊ต์ฅํ ๋นํจ์จ์ .
-> api ๋์๋ง๋ค ๋ฐ์ํ๋ ์ํ๋ฅผ ์ ์ญ์ผ๋ก ๊ด๋ฆฌํจ.
๋ํ api ์๋ต์์ ๋ฐ์ ๋ฐ์ดํฐ ์กฐ์, ๊ฐ๊ณต๊น์ง store์์ ๋ด๋น, ๊ด๋ จ ์ฝ๋๋ฅผ ํธ์ถ
์ด๋ Store๊ฐ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ ์ฝ๋ ๋ณด๋จ api ํต์ ์ ๋ด๋นํ๋ ์ฝ๋๋ก ๋ณด์..
Store์์ ๋น๋๊ธฐ ํต์ ์ ๊ฑท์ด๋ด๊ณ Client Side ์ ์ญ ์ํ ๊ด๋ฆฌ๋ก ํ๋ฐ๊ฟ
API ํต์ ๊ณผ sync๋ฅผ ๋ง์ถฐ์ผ ํ๋ฏ๋ก, Store ๋ฐ์์ ์๋ฒ์ ๊ด๋ จ๋ ์ํ ๊ด๋ฆฌ๋ฐฉ์ ๋ง๋ จ
-> ๊ทธ๋ฌ๋ฉด์, ์๋ฒ์ ๊ด๋ จ๋ ์ํ๋ ๋ง์น ์ ์ญ ์ํ์ฒ๋ผ ์ฌ์ฉ
React Query
: Server State๋ฅผ ๊ด๋ฆฌํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ, Server์ Client ์ฌ์ด ๋น๋๊ธฐ ๋ก์ง๋ค์ ์์ฝ๊ฒ ๋ค๋ฃฐ ์ ์๋ค.
Client์์๋ ์บ์ฑ, ๋ฐ์ดํฐ ์ ๋ฐ์ดํธ ๋ฐ ๊ด๋ฆฌ, lazy load์ ๊ฐ์ ์ต์ ํ, ์๋ต๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ๊ณต์ ํด์ ์ฌ์ฉํ ๊ฑด์ง ๊ณ ๋ฏผํด์ผํจ.
React Query๋ API ํต์ ์ ์ํ ์ฌ๋ฌ ํจ์๋ฅผ useQuery์ ๊ฐ์ Hook ์ธํฐํ์ด์ค๋ก ์ ๊ณต.
import {
useQuery,
useQueryClient,
QueryClient,
QueryClientProvider,
} from "@tanstack/react-query";
QueryClient : React Query๊ฐ Client์์ ๊ด๋ฆฌํ๋ Server State๋ค์ Key๋ฅผ ํตํด ๊บผ๋ด ์ฌ์ฉํ ์ ์๋ค.
const queryClient = new QueryClient();
QueryClientProvider : component to connect and provide a QueryClient to your application
function App() {
return (
<QueryClientProvider client={queryClient}>
<Menus />
</QueryClientProvider>
);
}
useQuery
: useQuery Hook์ผ๋ก ์ํ๋๋ Query ์์ฒญ์ HTTP METHOD GET ์์ฒญ๊ณผ ๊ฐ์ด
์๋ฒ์ ์ ์ฅ๋์ด ์๋ “์ํ”๋ฅผ ๋ถ๋ฌ์ ์ฌ์ฉํ ๋ ์ฌ์ฉํฉ๋๋ค.
const { data } = useQuery(
queryKey, // ์ด Query ์์ฒญ์ ๋ํ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์บ์ํ ๋ ์ฌ์ฉํ Unique Key (required)
// queryKey๋ก ์๋ฒ ์ํ(API Reaponse)๋ฅผ ๋ก์ปฌ์ ์บ์ํ๊ณ ๊ด๋ฆฌํฉ๋๋ค.
fetchFn, // ์ด Query ์์ฒญ์ ์ํํ๊ธฐ ์ํ Promise๋ฅผ Return ํ๋ ํจ์ (required)
options, // useQuery์์ ์ฌ์ฉ๋๋ Option ๊ฐ์ฒด (optional)
);
function Menus() {
const queryClient = useQueryClient();
const {data} = useQuery({
queryKey: ["getMenu"],
queryFn:() =>axios.get(
"https://jsonplaceholder.typicode.com/posts"
).then({ data }) => data),
},
});
}
useMutation
: ์๋ฒ์ ๋ฐ์ดํฐ ๋ณ๊ฒฝ ์์ฒญ์, Create, Update, Delete ์์ฒญ๊ณผ ๊ฐ์ด
API ํต์ ์ผ๋ก Server State์ Side Effect์ด ์ผ์ด๋๋ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
const mutation = useMutation({
mutationFn: postTodo,
onSuccess: () => {
// Invalidate and refetch
queryClient.invalidateQueries({ queryKey: ['todos'] })
},
})
const { mutate } = useMutation(
(suggest) => axios.post('/menu', { suggest }),
{
// Post ์์ฒญ์ด ์ฑ๊ณตํ๋ฉด ์ useQuery์ ๋ฐ์ดํฐ๋ฅผ ์ด๊ธฐํํฉ๋๋ค. (invalidateQueries)
// ๋ฐ์ดํฐ๊ฐ ์ด๊ธฐํ๋๋ฉด useQuery๋ ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ๋ถ๋ฌ์ต๋๋ค.
onSuccess: () => queryClient.invalidateQueries('posts'),
},
);
return (
<div>
<h1> Tomorrow's Lunch Candidates! </h1>
<ul>
{data.map((item) => (
<li key={item.id}> {item.title} </li>
))}
</ul>
<button
onClick={() =>
mutate({
id: Date.now(),
title: 'Toowoomba Pasta',
})
}
>
Suggest Tomorrow's Menu
</button>
</div>
);
}
์ฐธ๊ณ ๊ธ
https://tanstack.com/query/latest/docs/react/examples/react/basic
https://tanstack.com/query/latest/docs/react/quick-start
'javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ ์คํฌ๋ฆฝํธ ์ ๋ฌธ, ๊ธฐ๋ณธ ๋ฌธ๋ฒ (0) | 2023.09.22 |
---|---|
Array์ ํท๊ฐ๋ฆฌ๋ ๋ฉ์๋ ์ ๋ฆฌ(Array.from, arr.sort, arr.shift, arr.unshift) (0) | 2023.09.04 |
TypeScript ๊ฐ๋ (0) | 2023.03.27 |
Javascript ๋ฐ๋ณต๋ฌธ (forEach, map, filter, reduce) (0) | 2022.02.09 |
Javascript ๊ฐ์ฒด(๋ฌธ์ ๊ฐ์ฒด๋ชจ๋ธ(DOM),๋ธ๋ผ์ฐ์ ๊ฐ์ฒด๋ชจ๋ธ(BOM) ,๋ด์ฅ๊ฐ์ฒด) (0) | 2022.01.17 |