[React] 05. ๋ฆฌ์•กํŠธ Hooks

์†ก์šฐ๋“ ยท2021๋…„ 6์›” 8์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
5/23
post-thumbnail

์˜ค๋Š˜์€ React Hooks์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’šHooks

react 16.8 ๋ฒ„์ „์—์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ
ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์ƒํƒœ๊ด€๋ฆฌ๋‚˜ ๊ทธ ์™ธ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•จ
๋Œ€ํ‘œ์ ์œผ๋กœ useState,useEffect,useReducer,useMemo๋“ฑ์ด ์กด์žฌ

๐Ÿ’šuseState

stateํฌ์ŠคํŒ…์—์„œ ์ž ๊น ๋‹ค๋ฃจ์—ˆ๋˜ ๊ฐœ๋…์ธ๋ฐ์š”!
๋‹ค์‹œ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด, useState๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ state๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” Hooks์ž…๋‹ˆ๋‹ค.

๋จผ์ €, useState๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— import๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค.

import {useState} from "react";

useState๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด[์ƒํƒœ, ์ƒํƒœ๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜]๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š”๋ฐ์š”! ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” count์™€ setCountํ•จ์ˆ˜๊ฐ€ ๋˜๊ฒ ์ฃ ?

const Counter = () => {

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

    return(
        <div>
            <h1>ํ˜„์žฌ ์ˆซ์ž : {count}</h1>
            <button onClick={() => {setCount(count + 1)}}> +1 </button>
            <button onClick={() => {setCount(count - 1)}}> -1 </button>
        </div>
    );
};

+1๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด setCount(count + 1)์ด ์ž‘๋™๋˜๋ฉด์„œ count๋ฅผ +1๋งŒํผ ๊ฐฑ์‹ ํ•ด์ค๋‹ˆ๋‹ค.

๋ฐ˜๋Œ€๋กœ -1๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด setCount(count - 1)์ด ์ž‘๋™๋˜๋ฉด์„œ count๋ฅผ -1๋งŒํผ ์ƒˆ๋กœ์šด ์ƒํƒœ๋กœ ๊ฐฑ์‹ ํ•ด์ค๋‹ˆ๋‹ค.

๐Ÿ’šuseEffect

useEffect๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์„ค์ •ํ•ด์ฃผ๋Š” Hooks์ธ๋ฐ์š”!

useEffectํ•จ์ˆ˜๋Š” ์‹คํ–‰ํ•  ํ•จ์ˆ˜์™€ ๊ฒ€์‚ฌํ•˜๊ณ  ์‹ถ์€ ๊ฐ’ ๋‘๊ฐ€์ง€๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

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

const Counter = () => {

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

    useEffect(() => {
        console.log('์‹คํ–‰!');
    },[]);

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

์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด []๋นˆ ๋ฐฐ์—ด์„ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉด,

๐Ÿ’šuseReducer

useReducer๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํ™ฉ์— ๋”ฐ๋ฅธ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Hooks์ž…๋‹ˆ๋‹ค. useReducer๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ƒํƒœ ๋กœ์ง๊ณผ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถ„๋ฆฌ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

useReducer๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋จผ์ € import๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

import { useReducer } from 'react';

โœ” Reducer๋Š” ํ˜„์žฌ ์ƒํƒœ์™€ ์—…๋ฐ์ดํŠธ์— ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋‹ด์€ ์•ก์…˜ ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›์•„ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด state์™€ action์„ ์ธ์ž๋กœ ๊ฐ€์ง€๋Š” reducer()๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

function reducer(state, action) {

    switch (action.type) {
        case 'INCREMENT':
            return { count: state.count + 1 };
        case 'DECREMENT':
            return { count: state.count - 1 };
        default:
            return state;
    }
}

useReducer()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒซ๋ฒˆ์งธ ์ธ์ž์—๋Š” ์œ„์—์„œ ๋งŒ๋“  reducer()ํ•จ์ˆ˜๋ฅผ, ๋‘๋ฒˆ์งธ ์ธ์ž์—๋Š” reducer์˜ ์ดˆ๊ธฐ๊ฐ’์„ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

const Counter = () => {
    const [state, dispatch] = useReducer(reducer, { count: 0 });

    return (
        <div>
            <p>
                ํ˜„์žฌ ์ˆซ์ž : {state.count}
            </p>
            <button onClick={() => dispatch({ type: 'INCREMENT' })}>+1</button>
            <button onClick={() => dispatch({ type: 'DECREMENT' })}>-1</button>
        </div>
    );
};

๐Ÿ’šuseCallback

useCallback์€ ์ฃผ๋กœ ๋ Œ๋”๋ง ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ์š”. ์ƒ์„ฑํ•  ํ•จ์ˆ˜์™€ ๋ฐฐ์—ดํ˜•ํƒœ์˜ ์ฒดํฌํ•  ๊ฐ’์„ ์ธ์ž๋กœ ๋ฐ›์•„์š”!

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

const Counter = () => {

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

    const onChangeCount = useCallback(() => {
        console.log(count);
    },[]);

    return (
        <div>
            <p> ํ˜„์žฌ ์ˆซ์ž : {count}</p>
            <button onClick={() => setCount(count + 1)} onChange={onChangeCount}>
                +1
            </button>
        </div>
    );
};
export default Counter;

์œ„์— ์ฝ”๋“œ์™€ ๊ฐ™์ด []๋นˆ ๋ฐฐ์—ด์„ ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉด Counter์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง ๋  ๋•Œ ๋”ฑ ํ•œ๋ฒˆ๋งŒ onChangeCount()ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋Œ€๋กœ ๋ฐฐ์—ด์— ๊ฐ’์ด ์žˆ๋”ฐ๋ฉด, ํ•ด๋‹น ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ ๋  ๋•Œ๋งˆ๋‹ค ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

๐Ÿ’šuseMemo

useMemo๋Š” useCallback๊ณผ ์œ ์‚ฌํ•œ Hooks์ธ๋ฐ์š”. useMemo๋Š” ์ด์ „์— ์‚ฌ์šฉํ•œ ๊ฐ’์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ Œ๋”๋ง ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ๋ฆฌ์•กํŠธ์˜ ๋‹ค์–‘ํ•œ hooks์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด ๋ณด์•˜๋Š”๋ฐ์š”. hooks๋ฅผ ์ž˜ ํ™œ์šฉํ•˜์—ฌ ์ข€ ๋” ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค๋„๋ก ํ•ด๋ด์•ผ๊ฒ ์–ด์š”!

์ฐธ๊ณ  ์ž๋ฃŒ ๋ฐ ์‚ฌ์ดํŠธ

React ๊ณต์‹๋ฌธ์„œ
๋ฒจ๋กœํ”ผํŠธ์™€ ํ•จ๊ป˜ํ•˜๋Š” ๋ชจ๋˜ ๋ฆฌ์•กํŠธ

profile
๊ฐœ๋ฐœ ๊ธฐ๋ก๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€