ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

https://ko.reactjs.org/docs/hooks-overview.html

 

 

 

 

๐Ÿ“Œ State Hook

 

๋‹ค์Œ์€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๊ฐ’์ด 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์นด์šดํ„ฐ ์˜ˆ์‹œ ์ฝ”๋“œ์ด๋‹ค.

 

import React, {useState} from 'react';

function MyComponent() {
    // "count"๋ผ๋Š” ์ƒˆ ์ƒํƒœ ๋ณ€์ˆ˜ ์„ ์–ธ
    const[count, setCount] = useState(0);

    return (
        <div>
            <p>You click {count} times </p>
            <button onClick={() => setCount(count + 1)}>
                Click me
            </button>
        </div>
    );
}

export default MyComponent;
  • ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ useState๊ฐ€ ํ›…(Hook)์ด๋‹ค.
  • hook์„ ํ˜ธ์ถœํ•ด ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์•ˆ์— state ์ถ”๊ฐ€
  • ์ด state๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง ๋˜์–ด๋„ ๊ทธ๋Œ€๋กœ ์œ ์ง€

 

 

useState ํŠน์ง•

  • ํ˜„์žฌ์˜ state๊ฐ’๊ณผ ์ด ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ํ•จ์ˆ˜์˜ ์Œ
  • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋‚˜ ๋‹ค๋ฅธ ๊ณณ์— ํ˜ธ์ถœ ๊ฐ€๋Šฅ
  • class์˜ this.setState์™€ ๊ฑฐ์˜ ์œ ์‚ฌ
    • ์ด์ „ state์™€ ์ƒˆ๋กœ์šด state๋ฅผ ํ•ฉ์น˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ฐจ์ด์ 
    • setState Hook์˜ state๋Š” ๊ฐ์ฒด์ผ ํ•„์š” ์—†๋‹ค๋Š” ์ฐจ์ด์  (๊ฐ์ฒด์—ฌ๋„ ๋˜๊ธดํ•จ)
  • ์ธ์ž๋กœ ์ดˆ๊ธฐ state๊ฐ’์„ ํ•˜๋‚˜ ๋ฐ›์Œ (์œ„ ์˜ˆ์‹œ์—์„œ๋Š” 0์„ ๋ฐ›์Œ)
    • ์ด ์ดˆ๊ธฐ๊ฐ’์€ ์ฒซ๋ฒˆ์งธ ๋ Œ๋”๋ง์—๋งŒ ๋”ฑ ํ•œ๋ฒˆ ์‚ฌ์šฉ๋จ

 

 

์—ฌ๋Ÿฌ state ๋ณ€์ˆ˜ ์„ ์–ธํ•˜๊ธฐ

ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ State Hook์„ ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!

-> ๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด(destructuring) ๋ฌธ๋ฒ• ์‚ฌ์šฉ

  • useState๋กœ ํ˜ธ์ถœ๋œ state๋ณ€์ˆ˜๋“ค์„ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ํ• ๋‹น ๊ฐ€๋Šฅํ•˜๊ฒŒํ•จ
  • ๋ณ€์ˆ˜๋ช…์€ useState API์™€ ๊ด€๋ จ์ด ์—†๊ณ , ๋งค๋ฒˆ ๋ Œ๋”๋ง ํ• ๋•Œ useState๊ฐ€ ์‚ฌ์šฉ๋œ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰

 

 

๊ทธ๋Ÿฐ๋ฐ Hook์ด ๋ญ”๊ฐ€์š”?

ํ•จ์ˆ˜!

 

์–ด๋–ค ํ•จ์ˆ˜..?

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ React State์™€ ์ƒ๋ช…์ฃผ๊ธฐ ๊ธฐ๋Šฅ(lifecycle features)์„ "์—ฐ๋™(hook into)"ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

 

 

Hook ํŠน์ง•

  • ํด๋ž˜์Šค ์•ˆ์—์„œ๋Š” ๋™์ž‘X
  • ๋Œ€์‹  ํด๋ž˜์Šค ์—†์ด ๋ฆฌ์•กํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋„๊ตฌ
  • But ์ด๋ฏธ ์งœ๋†“์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ชจ์กฐ๋ฆฌ ์žฌ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ ๊ถŒ์žฅ X!
    ์ƒˆ๋กœ ์ž‘์„ฑํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ถ€ํ„ฐ Hook ์ด์šฉํ•˜๋ฉด ๋จ
  • ๋‚ด์žฅ Hook๋„ ์žˆ๊ณ , ๋‚ด๊ฐ€ Hook์„ ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Œ

 

 

 

 

 

โšก๏ธ Effect Hook

 

side effect๋ž€? (effects๋ผ๊ณ ๋„ ํ•จ)

์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ๊ตฌ๋…, DOM์„ ์ง์ ‘ ์กฐ์ž‘ํ•˜๋Š” ๋™์ž‘

(๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Œ. ๋ Œ๋”๋ง ๊ณผ์ •์—์„œ๋Š” ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†๋Š” ์ž‘์—…์ž„)

 

 

 

Effect Hook - useEffect

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ side effect๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ

(React class์˜ componentDidMount, componentDidUpdate, componentWillUnmount์™€ ๊ฐ™์€ ๋ชฉ์ ์œผ๋กœ ์ œ๊ณต๋˜์ง€๋งŒ,

 ํ•˜๋‚˜์˜ API๋กœ ํ†ตํ•ฉ๋œ ๊ฒƒ)

 

 

 

๋‹ค์Œ ์˜ˆ์‹œ๋Š” React๊ฐ€ DOM์„ ์—…๋ฐ์ดํŠธํ•œ ๋’ค์— ๋ฌธ์„œ์˜ ํƒ€์ดํ‹€์„ ๋ฐ”๊พธ๋Š” ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.

 

import React, { useState, useEffect } from 'react';

function MyComponent() {
    const [count, setCount] = useState(0);

    //componentDidMount, componentDidUpdate์™€ ๋น„์Šท
    useEffect(() => {
        // ๋ธŒ๋ผ์šฐ์ € API๋ฅผ ์ด์šฉํ•ด ๋ฌธ์„œ ํƒ€์ดํ‹€์„ ์—…๋ฐ์ดํŠธ
        document.title = `You clicked ${count} times`;
    });

    return (
        <div>
            <p>You click {count} times </p>
            <button onClick={() => setCount(count + 1)}>
                Click me
            </button>
        </div>
    );
}

export default MyComponent;
  • useEffect : React๋Š” DOM์„ ๋ฐ”๊พผ ๋’ค์—์„œ effect ํ•จ์ˆ˜ ์‹คํ–‰
  • Effects๋Š” ์ปดํฌ๋„ŒํŠธ ์•ˆ์— ์„ ์–ธ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— props์™€ state์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๊ธฐ๋ณธ์ ์œผ๋กœ React๋Š” ๋งค ๋ Œ๋”๋ง ์ดํ›„์— effects ์‹คํ–‰ (์ฒซ ๋ Œ๋”๋ง ํฌํ•จ)
  • ** ์ƒ๋ช…์ฃผ๊ธฐ์™€ ๋‹ค๋ฅธ ์ ์€ ๋‚˜์ค‘์— ์ž์„ธํžˆ ๋‹ค๋ฃจ๊ฒ ์Œ.

 

 

Effect๋ฅผ "ํ•ด์ œ"ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

-> ํ•ด์ œํ•˜๋Š” ํ•จ์ˆ˜ ๋ฐ˜ํ™˜ (์„ ํƒ์ )

 

 

๋‹ค์Œ์€ ์นœ๊ตฌ์˜ ์ ‘์† ์ƒํƒœ๋ฅผ ๊ตฌ๋…ํ•˜๋Š” effect๋ฅผ ์‚ฌ์šฉํ–ˆ๊ณ , ๊ตฌ๋…์„ ํ•ด์ง€ํ•จ์œผ๋กœ์จ effect ํ•ด์ œํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.

 

(ChatAPI ๋ถ€๋ถ„์— ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ, 4. Effect Hook ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๊ธฐ)

[ tlog.tammolo.com/blog/React-Hooks-cbf3d997cecb419fa570d0964d238629/ ] ํ˜น์‹œ ์ฐธ๊ณ ํ• ๊นŒ๋ด ์ฒจ๋ถ€ํ•˜๋Š” ๋งํฌ^.^

 

 

 

 

 

 

์ถ”๊ฐ€ +) effect๊ฐ€ ์žˆ๊ณ  ์—†๊ณ ์˜ ์ฐจ์ด์ 

โ— ํƒญ ๋ถ€๋ถ„ ์ž˜ ๋ด๋ด โ— ํŒŒ๋น„์ฝ˜ ์žˆ๋Š” ๋ถ€๋ถ„ โ—

 

1) effect ์žˆ๋Š” ๊ฒฝ์šฐ

2) effect ์—†๋Š” ๊ฒฝ์šฐ

 

 

 

 

 

 

 

๋ฒˆ์™ธ ) ' '์™€ ` `์˜ ์ฐจ์ด

  ' ' ` `
+ ์‚ฌ์šฉ ์—ฌ๋ถ€ + ์‚ฌ์šฉ ํ•ด์•ผํ•จ + ์‚ฌ์šฉ ์•ˆํ•ด๋„ ๋จ
์˜ˆ const userInfo = 'User info: ' + name + ' ' + surname + ' ' + telephone; const userInfo = `User info: ${name} ${surname} ${telephone}`;

 

 

 

 

 

โœŒ๏ธ Hook ์‚ฌ์šฉ ๊ทœ์น™

Hook์€ ๊ทธ๋ƒฅ JS ํ•จ์ˆ˜์ด์ง€๋งŒ, ๋‘ ๊ฐ€์ง€ ๊ทœ์น™์„ ์ค€์ˆ˜ํ•ด์•ผ ํ•จ

  • ์ตœ์ƒ์œ„(at the top level)์—์„œ๋งŒ Hook ํ˜ธ์ถœ
    • ๋ฐ˜๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ, ์ค‘์ฒฉ๋œ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์‹คํ–‰X
  • React ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ๋งŒ Hook ํ˜ธ์ถœ
    • ์ผ๋ฐ˜ JS์—์„œ Hook ํ˜ธ์ถœ X
    • ์ง์ ‘ ์ž‘์„ฑํ•œ custom Hook ๋‚ด์—์„œ๋Š” ํ›… ํ˜ธ์ถœ ๊ฐ€๋Šฅ

 

 

 

 

 

๐Ÿ’ก ๋‚˜๋งŒ์˜ Hook ๋งŒ๋“ค๊ธฐ

 

state ๊ด€๋ จ ๋กœ์ง์„ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

-> 1. higher-order components

-> 2. render props

 

 

์ด์™€ ๋‹ฌ๋ฆฌ Custom Hook์€ ์ปดํฌ๋„ŒํŠธ ํŠธ๋ฆฌ์— ์ƒˆ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ ๋„ ๊ฐ€๋Šฅ

 

 

 

 

 

๋Œ“๊ธ€