4. custom hook

์‹ค์Šต ๋ ˆํฌ - custom hook ๋ธŒ๋žœ์น˜

๋กœ์ง์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ œ์ผ ์‰ฌ์šด ๋ฐฉ๋ฒ•.

ํ‰๋ฒ”ํ•˜๊ฒŒ Extract Function์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” PascalCase๋กœ ์ด๋ฆ„์„ ๋ถ™์ธ๋‹ค๋ฉด,

Hook์€ use๋กœ ์‹œ์ž‘ํ•˜๋Š” camelCase๋กœ ์ด๋ฆ„์„ ๋ถ™์ด๋ฉด ๋œ๋‹ค.

์ด๋Ÿฐ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ณด์ž

import { useEffect, useState } from "react";
import TimerControl from "./components/TimerControl";

export default function App() {
 const [products, setProducts] = useState([]);
 
 useEffect(() => {
  const fetchProducts = async () => {
   const url = 'http://localhost:3000/products';
   const response = await fetch(url);
   const data = await response.json();
   setProducts(data.products);
  };

  fetchProducts();
 }, []);

  return (
  <>
   <TimerControl />
   <p>Hello, world!</p>
  </>

  );
}

์–˜๋ฅผ state, useEffect๋ฅผ ์˜ฎ๊ฒจ์„œ ๋งŒ๋“ ๋‹ค.


function useFetchProducts() {
 const [products, setProducts] = useState([]);
 
 useEffect(() => {
  const fetchProducts = async () => {
   const url = 'http://localhost:3000/products';
   const response = await fetch(url);
   const data = await response.json();
   setProducts(data.products);
  };

  fetchProducts();
 }, []);
 return products
}

export default function App() {
 const products = useFetchProducts()
 
  return (
  <>
   <TimerControl />
   <p>Hello, world!</p>
  </>

  );
}

~src/hooks/useFetchProducts ์— ๋‹ด์•„์คŒ ;;

// useFetchProducts.ts
import { useEffect, useState } from "react";

export default function useFetchProducts() {
 const [products, setProducts] = useState([]);
 
 useEffect(() => {
  const fetchProducts = async () => {
   const url = 'http://localhost:3000/products';
   const response = await fetch(url);
   const data = await response.json();
   setProducts(data.products);
  };

  fetchProducts();
 }, []);
 return products
}
// App.tsx
import { useEffect, useState } from "react";
import TimerControl from "./components/TimerControl";
import useFetchProducts from "./hooks/useFetchProducts";


export default function App() {
 const products = useFetchProducts()

  return (
  <>
   <TimerControl />
   <p>Hello, world!</p>
  </>

  );
}

setState ํ•จ์ˆ˜๋Š” ์ˆจ๊ธฐ๋Š” ํ˜•ํƒœ. ์บก์Аํ™”ํ•œ ํ˜•ํƒœ์ด๋‹ค.

  • ์ปดํฌ๋„ŒํŠธ ์ฝ”๋“œ๋Š” ๋ช…ํ™•ํ•ด์ง€๊ณ  setState ํ•จ์ˆ˜๊ฐ€ ์‹ค์ˆ˜๋กœ ์ž˜๋ชป ์“ฐ์ผ ๋ฌธ์ œ๋ฅผ ํ•ด์†Œ.

  • ์—ฌ๋Ÿฌ state๋ฅผ ํ•œ๋ฒˆ์— ๋ฌถ๋Š”๊ฒƒ๋„ ๊ฐ€๋Šฅ.

Hook ๊ทœ์น™

Hook ํ˜ธ์ถœ์€ ๊ทœ์น™์ด ์žˆ์–ด์„œ ๋‹จ์ˆœํ•˜๊ฒŒ ์“ฐ๋„๋ก ๋…ธ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

  1. Function Component ๋ฐ”๋กœ ์•ˆ์ชฝ(ํ•จ์ˆ˜์˜ ์ตœ์ƒ์œ„)์—์„œ๋งŒ ํ˜ธ์ถœ. ์กฐ๊ฑด๋ฌธ์ด๋‚˜ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ ํ•˜๋ฉด ์•ˆ๋จ. ์ˆœ์„œ ๋ณด์žฅ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํ•จ์ˆ˜๋Š” ์‹ค์ œ๋กœ ์ƒํƒœ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†์ง€๋งŒ react hook์€ index ๊ธฐ๋ฐ˜์˜ ์™ธ๋ถ€ ์ƒํƒœ์— ๊ฐ’์„ ์ €์žฅํ•ด๋†“๊ณ  ๋งˆ์น˜ ๊ทธ ํ•จ์ˆ˜๊ฐ€ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ํšจ๊ณผ๋ฅผ ๋‚ด๋Š” ์›๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

hook์— ์ œ์•ฝ์ด ์žˆ๋Š” ์ด์œ 

  1. Function Component ๋˜๋Š” Custom Hook์—์„œ๋งŒ ํ˜ธ์ถœ. ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒํƒœ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ์ปจ์…‰์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹น์—ฐ..

Last updated