ν μ΄ νλ‘μ νΈμμ Open APIμμ λΆλ¬μ¨ λ°μ΄ν°λ₯Ό νν°λ§νλ λ‘μ§μ ꡬννκ² λμλ€.
μμ κ°μ ννμ μ»΄ν¬λνΈμλλ°, μ΄λ»κ² νν°κ°μ λ°μν΄μΌ ν μ§μ λν΄ κ³ λ―Όμ΄ λ§μλ€.
λ¬Όλ‘ νν°λ§ μ’
λ₯κ° λ§μμλ λ§μ§λ§, κ°μ₯ κ³ λ―Όμ΄μλ μ μ
Open APIμμ κ°μ λ°μμ€κΈ° λλ¬Έμ μνλ νν° κ°μ νλ‘ νΈμμ μ§μ ꡬνν΄μΌ νλ€λ μ μ΄μλ€.
μ΄λ»κ² νλ©΄ ν΄λΌμ΄μΈνΈμμ νν°λ§ λμ§ μμ μ±λ‘ λ°μ μλ΅ κ°μ μ μ ν λ,
μ΅μνμ API μμ²κ³Ό μ΅λνμ νΌν¬λ¨Όμ€λ₯Ό λΌ μ μμκΉ?
μ΄μ λν΄ react-queryμ λ°μ΄ν°λ₯Ό λ³ννλ μ’μ λ°©λ²λ€μ΄ μμ΄μ μ 리ν΄λ³΄κ³ μ νλ€.
π©βπ» κ°λ₯νλ€λ©΄ λ°±μλμμ μ²λ¦¬νλ κ²λ μ’μ λ°©λ²μ
λλ€.!
useQuery
μ μ λ¬ν΄μΌ νλ ν¨μμ΄λ€.const fetchAnimalList = async (): Promise<Animal[]> => {
const response = await axios.get('/animalList/Seoul')
const data: Animal[] = response.data
return data.map((animal) => animal.name.toUpperCase())
}
export const useGetAnimalList = () =>
useQuery({
queryKey: ['animalList'],
queryFn: fetchAnimalList,
})
β μ₯μ
β λ¨μ
const fetchAnimalList = async (): Promise<Animal[]> => {
const response = await axios.get('/animalList/Seoul')
return response.data
}
export const useGetAnimalList = () => {
const queryInfo = useQuery({
queryKey: ['animalList'],
queryFn: fetchAnimalList,
})
return {
...queryInfo,
data: queryInfo.data?.map((animal) => animal.name.toUpperCase()),
}
}
π©βπ» useMemoλ₯Ό μ¬μ©νμ¬ μ΄λ¬ν λ¬Έμ λ₯Ό ν΄κ²°ν΄λ³Ό μ μμ΅λλ€.
useMemo
λ‘ μ΅μ ννκΈ°
μ΄λ μ£Όμν μ μ, μ’ μμ±μ μ΅λν μ’κ² μ μν΄μΌ νλ€λ κ²μ΄λ€.
data: React.useMemo(
() => queryInfo.data?.map((animal) => animal.name.toUpperCase()),
[queryInfo]
)
useMemo
λ₯Ό μ¬μ©νλ μλ―Έκ° μλ€.queryInfo
λ₯Ό μμ‘΄μ± λ°°μ΄μ μΆκ°ν κ²½μ° κ²°κ΅ λ§€ λ λλ§λ§λ€ λ€μ λ³νμ΄ λμνλ κ²μ λμΌνκΈ° λλ¬Έμ΄λ€.μλμ κ°μ λ°©μμΌλ‘ ꡬνν΄λ³΄μ.
export const useGetAnimalList = () => {
const queryInfo = useQuery({
queryKey: ['animalList'],
queryFn: fetchAnimalList,
})
return {
...queryInfo,
data: React.useMemo(
() => queryInfo.data?.map((animal) => animal.name.toUpperCase()),
[queryInfo.data]
),
}
}
queryInfo
λ΄λΆμ dataλ 무μΈκ°κ° μ€μ λ‘ λ³νλ κ²½μ°, λ€μ λ§ν΄ λ€μ λ°μ΄ν°μ λ³νμ μννκ³ μΆμ μν©μ΄ μλλΌλ©΄ μ°Έμ‘°μ μΌλ‘ μμ νλ€.
μ¬κΈ°μ λ°μ΄ν° λ³νκ³Ό ν¨κ» μΆκ°λ‘ μ¬μ©νκ³ μΆμ λ‘μ§μ λ£μ΄μ£Όμ΄λ μ’λ€.
- νμ§λ§ λ°μ΄ν°κ° undefined
λ μ μμΌλ―λ‘ μ΅μ
λ 체μ΄λμ κΌ κ°μ΄ μ¬μ©ν΄μ£Όλλ‘ νμ.
μΆκ°λ‘, React Queryκ° v4λΆν° Tracked Queries
μΆκ°ν΄μ μμ ...queryInfo
μ κ°μ ꡬ쑰λΆν΄ν λΉ μ½λλ μ§μνλ μΆμΈκ° λμμμ μ£Όμνμ.
β μ₯μ
useMemo
λ₯Ό μ¬μ©νμ¬ μ΅μ νν μ μλ€.β λ¨μ
undefined
λ μ μλ€.select: (data: TData) => unknown
optionalν μμ±μ΄λ€.
query ν¨μλ‘ λ°νλλ λ°μ΄ν°μ μΌλΆλ₯Ό λ³ννκ±°λ μ νν μ μλ€.
λ°νλλ λ°μ΄ν°μ κ°μ μν₯μ λ―ΈμΉμ§λ§ 쿼리 cacheμ μ μ₯λλ κ°μλ μν₯μ λ―ΈμΉμ§ μλλ€.
λ°μ΄ν°κ° μ‘΄μ¬ν λμλ§ selectorκ° νΈμΆλλ―λ‘ undefined
κ°μ λν΄ κ±±μ νμ§ μμλ λλ€.
μλ₯Ό λ€μ΄, λλ¬Ό ꡬλΆμ΄ 'κ°' μ ν΄λΉνλ λλ¬Όμ νν°λ§νλ€κ³ κ°μ ν΄λ³΄μ.
const useGetAnimalList = (queryKeys, func, filterLogic) => useQuery(
[queryKeys],
() => func(),
{
select: (animals) => animals.filter(filterLogic)
}
)
(animal) => animal.category === 'κ°'
κ° λ€μ΄κ° μ μμ κ²μ΄κ³ , category
μ 'κ°'
λΆλΆλ§ μ¬μ¬μ©μ΄ κ°λ₯νλλ‘ λ€λ₯Έ κ°μΌλ‘ λ°κΎΌλ€λ©΄ νΈλ¦¬νκ² useQueryλ§ μ¬μ©ν΄μ νν°λ§ λ‘μ§μ ꡬνν μ μλ€.
useCallback
μ¬μ©νκΈ°
select: React.useCallback((animals) => animals.filter(filterLogic), [])
ν¨μμ μ°Έμ‘°λ₯Ό μμ μ μΌλ‘ λ§λ€κΈ°
const transFormLogic = (data: Animal[], filterLogic) =>
data.map((animals) => animals.filter(filterLogic))
const useGetAnimalList = (queryKeys, func, filterLogic) => useQuery(
[queryKeys],
() => func(),
{
select: transFormLogic
}
)
export const useGetAnimalList = (select) =>
useQuery({
queryKey: ['animalList'],
queryFn: fetchAnimalList,
select,
})
export const useOnlyCategory = (targetCategory: string) =>
useGetAnimalList((data) => data.map((animal) => animal.category))
export const useFindAnimal = (targetName: string) =>
useGetAnimalList((data) => data.find((animal) => animal.name === targetName))
useGetAnimalList
μ 컀μ€ν
selectorλ€μ λ겨μ€μΌλ‘μ¨ μλ³Έ λ°μ΄ν°μμ νμν λ°μ΄ν°λ§ λ°μ μΈ μ μλ€.useOnlyCategory
λ₯Ό ν΅ν΄ μΉ΄ν
κ³ λ¦¬ μ λ³΄λ§ κ΅¬λ
νκ³ μλ μ»΄ν¬λνΈλ 리λ λλ§λμ§ μλλ€.isFetching
μ μν λ³νμ λ°λΌ λ λ² λ λλ§ λ κ°λ₯μ±μ μλ€.β μ₯μ
β λ¨μ
π©βπ» μ΄μ²λΌ μ¬λ¬ λ°©μλ€μ μ°Ύμ보면μ select μ΅μ
μ΄ μνλ λ‘μ§μ ꡬνμ κ°μ₯ μ ν©νλ€λ κ²°λ‘ μ λ΄λ Έλ€!