๐Ÿ’ป DEV/React-Query

[ReactQuery] (3) ReactQuery - customHooks (feat. Nextjs)

Rising Oneโ˜… 2025. 3. 15. 21:40
728x90
๋ฐ˜์‘ํ˜•
SMALL

Tanstack (ReactQuery) ๋Œ€ํ‘œ์ด๋ฏธ์ง€

 

 

์ ‘๊ทผ (์š”์•ฝ)

reactQuery๋ฅผ ํ™œ์šฉํ•ด custom hooks์„ ์ž‘์„ฑํ•œ๋‹ค. (queryKey์™€ queryFn)  

 

  1. queryKey Object (queryKey Namespace) ํŒจํ„ด์œผ๋กœ ์ •๋ฆฌ

- queryKey : ๋ฐ์ดํ„ฐ ์š”์ฒญ์„ ์‹๋ณ„ํ•˜๋Š” ์ค‘์š”ํ•œ ์—ญํ• 

- ์ฟผ๋ฆฌ ํ‚ค ๋„ค์ž„์ŠคํŽ˜์ด์Šค ํŒจํ„ด์„ ํ™œ์šฉํ•œ ํšจ์œจ์  ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ

- ./react-query/index.ts

 

  2. hooks ํด๋” ๋‚ด์— customHooks ์ž‘์„ฑ => useExampleQuery.ts

- app/hooks/queries/useExampleQuery.ts

- app/hooks/queries/mutation/useExampleMutation.ts

 

  3. useExampleQuery.ts (CustomHooks ์ฝ”๋“œ ํ˜•์‹)

- Data Fetching์„ ์บก์Аํ™”ํ•œ react-query๊ธฐ๋ฐ˜ Custom Hook

- ์žฌ์‚ฌ์šฉ์„ฑ์„ ๊ณ ๋ คํ•œ react-query custom hook ํŒจํ„ด

- useQuery๋ฅผ ํ™œ์šฉํ•œ ์ปค์Šคํ…€ ํ›…์„ ํ†ตํ•ด API ์š”์ฒญ์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๊ธฐ


๋ฐฉ๋ฒ•

  0. Custom Hooks

## Custom Hooks

- ๋Œ€๊ทœ๋ชจ ์•ฑ์—์„œ๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ์œ ํ˜•์— ๋Œ€ํ•ด ์ปค์Šคํ…€ ํ›…์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ 

- ์žฅ์ 1 : (๋‹ค์ค‘ ์ปดํฌ๋„ŒํŠธ ์•ก์„ธ์Šค O)

์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ๋“ค์˜ ๋ฐ์ดํ„ฐ์— ์•ก์„ธ์Šคํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ useQuery ํ˜ธ์ถœ์„ ๋‹ค์‹œ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง„๋‹ค.

- ์žฅ์ 2 : (Query Key ํ˜ผ๋™ X) ์—ฌ๋Ÿฌ ๊ฐœ์˜ Query ํ˜ธ์ถœ๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ ์ค‘์ธ ํ‚ค๊ฐ€ ํ˜ผ๋™ ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, Custom hooks๋ฅผ ์‚ฌ์šฉํ•ด ๋งค๋ฒˆ ํ˜ธ์ถœ์„ ํ•˜๋ฉด ํ‚ค ํ˜ผ๋™ ์œ„ํ—˜ X

- ์žฅ์ 3 : (QueryFn ํ˜ผ๋™ X) ์‚ฌ์šฉํ•˜๋ ค๋Š” QueryFn (์ฟผ๋ฆฌ ํ•จ์ˆ˜) ํ˜ผ๋™ ์œ„ํ—˜ X

- ์žฅ์ 4 : ๋””์Šคํ”Œ๋ ˆ์ด ๊ณ„์ธต์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์˜ implementation์„ ์ถ”์ถœ - implementation์„ ๋ณ€๊ฒฝํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ•œ ๊ฒฝ์šฐ, Hooks๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋จ - ์ปดํฌ๋„ŒํŠธ ์—…๋ฐ์ดํŠธ ํ•„์š”๋„ ์—†๋‹ค


 

  1. queryKey Object (queryKey Namespace) ํŒจํ„ด์œผ๋กœ ์ •๋ฆฌ

import {QueryClient} from "@tanstack/react-query";

// queryKey Object ํŒจํ„ด(=queryKey Namespace) ํ˜•์‹์œผ๋กœ ์—ฐ์†์ ์œผ๋กœ ์ž‘์„ฑ
export const queryKeys = {
  examples: {
    exam1: 'example1',
    exam2: 'example2',
  },
  samples: {
    sample1: 'sample-a',
    sample2: 'sample-b',
  }
};

export const queryClient = new QueryClient();

 

- queryKey : ๋ฐ์ดํ„ฐ ์š”์ฒญ์„ ์‹๋ณ„ํ•˜๋Š” ์ค‘์š”ํ•œ ์—ญํ• 

- ์ฟผ๋ฆฌ ํ‚ค ๋„ค์ž„์ŠคํŽ˜์ด์Šค ํŒจํ„ด์„ ํ™œ์šฉํ•œ ํšจ์œจ์  ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ

- ( ./react-query/index.ts )

 


  2. hooks ํด๋” ๋‚ด์— customHooks ์ž‘์„ฑ => useExampleQuery.ts

- app/hooks/queries/useExampleQuery.ts

- app/hooks/queries/mutation/useExampleMutation.ts

 


  3. useExampleQuery.ts (CustomHooks ์ฝ”๋“œ ํ˜•์‹)

// CustomHooks๋ฅผ ์œ„ํ•œ ์˜ˆ์‹œ

import {useQuery} from "@tanstack/react-query";
import {queryKeys} from "@/react-query";

export default function useExampleQuery() {
  return useQuery({
    queryKey: [queryKeys.examples.exam1],
    queryFn: () => getExampleInfo(),
  });
}

export async function getExampleInfo() {
  // TODO : Fetch ๊ตฌ๋ฌธ
  // const response = await clientFetch<>({ url: `/`})
  // return response?.data ? response.data : undefined;
}

 

- Data Fetching์„ ์บก์Аํ™”ํ•œ react-query๊ธฐ๋ฐ˜ Custom Hook

- ์žฌ์‚ฌ์šฉ์„ฑ์„ ๊ณ ๋ คํ•œ react-query custom hook ํŒจํ„ด

- useQuery๋ฅผ ํ™œ์šฉํ•œ ์ปค์Šคํ…€ ํ›…์„ ํ†ตํ•ด API ์š”์ฒญ์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๊ธฐ

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•
LIST