์ฟผ๋ฆฌ ํจ์๋ promise๋ฅผ ๋ฐํํ๋ ๋ชจ๋ ํจ์๊ฐ ๋ ์ ์๋ค. ์ด promise๋ ๋ฐ์ดํฐ๋ฅผ reslove(์ฑ๊ณต์ ์ผ๋ก ์ฒ๋ฆฌ)ํ๊ฑฐ๋ ์๋ฌ๋ฅผ throwํด์ผ ํ๋ค.
๋ค์์ ๋ชจ๋ ์ ํจํ ์ฟผ๋ฆฌ ํจ์ ์ค์ ์ ์์์ด๋ค.
useQuery({ queryKey: ['todos'], queryFn: fetchAllTodos })
useQuery({ queryKey: ['todos', todoId], queryFn: () => fetchTodoById(todoId) })
useQuery({
queryKey: ['todos', todoId],
queryFn: async () => {
const data = await fetchTodoById(todoId)
return data
},
})
useQuery({
queryKey: ['todos', todoId],
queryFn: ({ queryKey }) => fetchTodoById(queryKey[1]),
})
์๋ฌ ์ฒ๋ฆฌ ๋ฐ ๋ฐ์์ํค๊ธฐ
Tanstack Query๊ฐ ์ฟผ๋ฆฌ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค๊ณ ์ธ์งํ๋ ค๋ฉด, ์ฟผ๋ฆฌ ํจ์๋ ๋ฐ๋์ ์๋ฌ๋ฅผ throwํ๊ฑฐ๋ ๊ฑฐ๋ถ๋ Promise๋ฅผ ๋ฐํํด์ผ ํ๋ค. ์ฟผ๋ฆฌ ํจ์ ๋ด์์ ๋ฐ์ํ ๋ชจ๋ ์๋ฌ๋ ์ฟผ๋ฆฌ์ error ์ํ์ ์ ์ฅ๋๋ค.
const { error } = useQuery({
queryKey: ['todos', todoId],
queryFn: async () => {
if (somethingGoesWrong) {
throw new Error('Oh no!')
}
if (somethingElseGoesWrong) {
return Promise.reject(new Error('Oh no!'))
}
return data
},
})
fetch์ ๊ฐ์ด ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ฌ๋ฅผ ๋ฐ์์ํค์ง ์๋ ํด๋ผ์ด์ธํธ์ ํจ๊ป ์ฌ์ฉํ๊ธฐ
axios๋ graphql-request ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ HTTP ํธ์ถ์ด ์คํจํ๋ฉด ์๋์ผ๋ก ์๋ฌ๋ฅผ ๋ฐ์์ํค์ง๋ง, fetch์ ๊ฐ์ ์ผ๋ถ ์ ํธ๋ฆฌํฐ๋ ๊ทธ๋ ์ง ์๋ค. ์๋ฅผ ๋ค์ด, fetch๋ 404๋ 500 ์๋ฌ๊ฐ ๋ฐ์ํด๋ ์๋ฌ๋ฅผ throwํ์ง ์๊ณ ์ ์ ์๋ต์ ๋ฐํํ๋ค.
์ด ๊ฒฝ์ฐ, ์ง์ ์๋ฌ๋ฅผ ๋ฐ์์์ผ์ผ ํ๋ค. ๋ค์์ fetch API๋ฅผ ์ฌ์ฉํ ๋ ์ด๋ฅผ ์ฒ๋ฆฌํ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ด๋ค.
useQuery({
queryKey: ['todos', todoId],
queryFn: async () => {
const response = await fetch('/todos/' + todoId)
if (!response.ok) {
throw new Error('Network response was not ok')
}
return response.json()
},
})
์ฟผ๋ฆฌ ํจ์ ๋ณ์
์ฟผ๋ฆฌ ํค๋ ๊ฐ์ ธ์ฌ ๋ฐ์ดํฐ๋ฅผ ๊ณ ์ ํ๊ฒ ์๋ณํ๋ ์ฉ๋๋ฟ๋ง ์๋๋ผ, ์ฟผ๋ฆฌ ํจ์์ QueryFunctionContext์ ์ผ๋ถ๋ก ํธ๋ฆฌํ๊ฒ ์ ๋ฌ๋๋ค. ํญ์ ํ์ํ ๊ฒ์ ์๋์ง๋ง, ์ด ๊ธฐ๋ฅ ๋๋ถ์ ํ์ํ๋ค๋ฉด ์ฟผ๋ฆฌ ํจ์๋ฅผ ์ธ๋ถ๋ก ๋ถ๋ฆฌํ๋ ๊ฒ์ด ๊ฐ๋ฅํด์ง๋ค.
function Todos({ status, page }) {
const result = useQuery({
queryKey: ['todos', { status, page }],
queryFn: fetchTodoList,
})
}
// queryKey๋ฅผ ๊ตฌ์กฐ ๋ถํด ํ ๋นํ์ฌ status์ page ๋ณ์์ ์ ๊ทผํ ์ ์๋ค.
function fetchTodoList({ queryKey }) {
const [_key, { status, page }] = queryKey // ์ฒซ ๋ฒ์งธ ์์๋ ๋ฌด์ํ๊ณ , ๋ ๋ฒ์งธ ๊ฐ์ฒด์์ status์ page๋ฅผ ์ถ์ถ
return new Promise()
}
QueryFunctionContext
QueryFunctionContext๋ ๊ฐ ์ฟผ๋ฆฌ ํจ์์ ์ ๋ฌ๋๋ ๊ฐ์ฒด์ด๋ฉฐ, ๋ค์๊ณผ ๊ฐ์ ์์ฑ๋ค์ ํฌํจํ๋ค.
- queryKey: QueryKey
- ์ฟผ๋ฆฌ๋ฅผ ์๋ณํ๋ ํค์ด๋ค.
- client: QueryClient
- ํ์ฌ QueryClient์ ์ธ์คํด์ค์ด๋ค.
- signal?: AbortSignal
- TanStack Query์์ ์ ๊ณตํ๋ AbortSignal ์ธ์คํด์ค์ด๋ค.
- Query Cancellation์์ ์ฌ์ฉ๋ ์ ์๋ค.
- meta: Record<string, unknown> | undefined
- ์ฟผ๋ฆฌ์ ๋ํ ์ถ๊ฐ ์ ๋ณด๋ฅผ ๋ด์ ์ ์๋ ์ ํ์ ํ๋์ด๋ค.
๋ํ, Infinite Queries์์๋ ๋ค์ ์ต์ ๋ค์ด ์ถ๊ฐ๋ก ์ ๋ฌ๋๋ค.
- PageParam: TPageParam
- ํ์ฌ ํ์ด์ง๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐ ์ฌ์ฉ๋ ํ์ด์ง ํ๋ผ๋ฏธํฐ์ด๋ค.
- direction: 'forward' | 'backward'
- deprecated
- ํ์ฌ ํ์ด์ง๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐฉํฅ์ ์ ๊ทผํ๋ ค๋ฉด getNextPageParam์ด๋ getPreviousPageParam ํจ์์์ pageParam ๊ฐ์ฒด์ direction ์ ๋ณด๋ฅผ ์ง์ ์ถ๊ฐํด์ ์ฌ์ฉํ ์ ์๋ค.
https://tanstack.com/query/latest/docs/framework/react/guides/query-functions
Query Functions | TanStack Query React Docs
A query function can be literally any function that returns a promise. The promise that is returned should either resolve the data or throw an error. All of the following are valid query function conf...
tanstack.com
'Frontend > TanStack Query' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Network Mode (0) | 2025.10.24 |
|---|---|
| Query Options (0) | 2025.10.23 |
| Query Keys (0) | 2025.10.21 |
| Queries (0) | 2025.10.20 |
| ์ TanStack Query๋ฅผ ์ฌ์ฉํ ๊น? (0) | 2025.10.19 |