front/react

[react] React Hook 2 (useContext, useRef)

๋ฐฐ๊ณ ํŒŒ์š” 2026. 1. 6. 11:26
728x90


๐Ÿ“ useContext

  • ์ƒํƒœ๋ฅผ ์ „์—ญ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•จ.
  • useState Hook ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๊นŠ์ด ์ค‘์ฒฉ๋œ ์ปดํฌ๋„ŒํŠธ ๊ฐ„ ์ƒํƒœ๋ฅผ ๋” ์‰ฝ๊ฒŒ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Œ.
  • ์ƒํƒœ๋Š” ํ•ด๋‹น ์ƒํƒœ์— ์•ก์„ธ์Šคํ•ด์•ผํ•˜๋Š” ์Šคํƒ์˜ ๊ฐ€์žฅ ๋†’์€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ณด๊ด€๋˜์–ด์•ผ ํ•จ.


๐Ÿ“ Props drilling

 

 

  • ์ปดํฌ๋„ŒํŠธ3 ์—์„œ User ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, 
  • ๋ฐ˜๋“œ์‹œ ๊ทธ ์ „์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ2์—์„œ 3์œผ๋กœ User ๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ์ œ๋Œ€๋กœ 3์—์„œ User๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹จ ๋ง์ž„. 
  • ๋งŒ์•ฝ 10๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์„œ component ๊ฐ€ ์žˆ๋‹ค๋ฉด,
  • 10๋‹จ๊ณ„์˜ Props drilling ์ด ํ•„์š”ํ•œ ๊ฒƒ์ž„.
  • --> ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ, useContext ๊ฐ€ ์ƒ๊ฒจ๋‚จ.

 

 

 

๐Ÿ“ useContext ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

import { createRoot } from "react-dom/client";
import { useState, createContext, useContext } from "react";

const UserContext = createContext(); // useContext ์‚ฌ์šฉํ•ด๋ณด๊ธฐ.



function Component1() {
    const [user, setUser] = useState("Linux");
    const [user2, setUser2] = useState("test");
    return (
        <>
            <UserContext.Provider value={user2}>
                <h1>{`์•ˆ๋…• ๋‚˜๋Š” ${user2} !`}</h1>
                <h1>{`์•ˆ๋…• ๋‚˜๋Š” ${user} !`}</h1>
                <Component2 user={user}/>
            </UserContext.Provider>
        </>
    );
}

function Component2( {user} ){

    return (
        <>
            <h1>component 2</h1>
            <Component3 user={user}/>
        </>
    );
}

function Component3( {user} ){
    const user2 = useContext(UserContext);
    return (
        <>
            <h1>component 3</h1>
            <h2>{`Hello ${user} again!!`}</h2>
            <h2>{`Hello ${user2} again!!`}</h2>
        </>
    );
}

createRoot(document.getElementById('react4')).render(< Component1/>);
  • import { createContext, useContext } from "react";
  • const UserContext = createContext(); 
    • ์ฒ˜์Œ์— UserContext ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ useContext ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์„ ์–ธํ•œ ๊ฒƒ.
  • <UserContext.Provider value={user2}>
        <Component2 />
    </UserContext.Provider>
    • ์ž๋™์œผ๋กœ Component2 ์— Props ๋ฅผ ๋„ฃ์–ด์„œ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ component2์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ.
    • ๊ผญ! <UserContext.Provider> ๋กœ ๊ฐ์‹ธ์ค˜์•ผํ•จ.
  • const user2 = useContext(UserContext);
    • ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ฒ˜์Œ์— ์„ ์–ธํ•œ UserContext ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ํ•ด์ฃผ๊ณ  ์จ์•ผํ•จ.

 


 

๐Ÿ“ useRef

  • state์™€ ๊ฐ™์ด ์–ด๋– ํ•œ ๊ฐ’์„ ์œ ์ง€ํ•˜๋Š” ๋ณ€์ˆ˜๋กœ์จ ์‚ฌ์šฉ๋จ.
  • ๋‹ค๋งŒ, state์™€ ๋‹ค๋ฅธ ์ ์€ ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ์Œ.
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ querySelector ์™€ ๊ฐ™์ด Html DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ๋จ.
  • ---------------------------------------------------------------------------------------------------  
  • ์„ ์–ธ
    • const a = useRef(์ดˆ๊ธฐ๊ฐ’);
  • ์ €์žฅ์€ ๊ฐ์ฒด๋กœ ์ €์žฅ๋œ
    • {current : ์ดˆ๊ธฐ๊ฐ’}
  • ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ๋จ.
    • a.current
  • ์›๋ž˜ useState ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š”, --> ๋ Œ๋”๋ง ๋˜๋ฉด ๊ฐ’์ด ์ดˆ๊ธฐํ™”๊ฐ€ ๋จ.
  • useRef ๋Š”
    • ๊ฐ’์ด ๋ณ€ํ•ด๋„ ๋ Œ๋”๋ง์ด ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ .
    • ์ผ๋ฐ˜ ๋ณ€์ˆ˜์™€์˜ ์ฐจ์ด๋Š” ์ „ lifecycle ๋™์•ˆ ๊ฐ’์ด ์‚ด์•„ ์žˆ๋‹ค๋Š” ์ ..
    • --> ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ด์•„์žˆ๋Š” ๋™์•ˆ์—๋Š” ๊ฐ’์ด ์œ ์ง€๊ฐ€ ๋จ.


๐Ÿ“ ๋ฐฉ๋ฒ• 1 : useState ์‚ฌ์šฉํ•ด๋ณด๋ฉด?

useEffect( () => {
        setRenderValue(renderValue + 1);
        console.log(renderValue); // ์—ฌ๊ธฐ ๋ฌดํ•œ๋ฃจํ”„ ๋Œ์Œ
    });
  • ์ตœ์ดˆ ๋ Œ๋”๋ง ํ•˜๋ฉด?
    • useEffect ํ›…์ด ๋™์ž‘์„ ํ•จ.
    • ์˜์กด์„ฑ ๋ฐฐ์—ด์„ ์ƒ๋žตํ•˜๊ฒŒ ๋˜๋ฉด, 
      ๋งจ ์ฒ˜์Œ์— ๋žœ๋”๋ง ๋  ๋•Œ, state ๊ฐ’์ด ๋ณ€ํ•˜๊ฒŒ ๋จ. 
      setRenderValue ๊ฐ€ ์ž‘๋™ํ•ด์„œ --> renderValue ๊ฐ’์ด ๋ณ€ํ•˜๊ฒŒ ๋˜์ง€.
      ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์‹œ ๋ Œ๋”๋ง์ด ์ž‘๋™ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, 
      ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด์„œ ์นด์šดํŠธ๊ฐ€ ๋ฌดํ•œ๋ฃจํ”„๋ฅผ ๋Œ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž„.

 

 

 

 

๐Ÿ“ ๋ฐฉ๋ฒ• 2 : ์ผ๋ฐ˜ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•ด๋ณด๋ฉด?

let cnt = 0;

useEffect( () => {
    cnt++;
    console.log(cnt); // ๊ณ„์†ํ•ด์„œ 1๋งŒ ๋‚˜์˜ด.
});
  • ์—ฌ๊ธฐ์„œ๋Š” ๊ณ„์†ํ•ด์„œ 1๋งŒ ์ฐํž ๊บผ์ž„.
  • ์ผ๋ฐ˜ ๋ณ€์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ๊ฐ’์ด ์œ ์ง€๊ฐ€ ๋˜์ง€ ์•Š์Œ., useState Hook์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ...
  • ์ด ์ฝ”๋“œ์—์„œ๋Š” 
    input์— ๊ฐ’์„ ๋„ฃ์„ ๋•Œ๋งˆ๋‹ค, ๋ Œ๋”๋ง ๋˜๋ฉด์„œ --> ์ดˆ๊ธฐํ™” ๋˜๊ณ  --> cnt++ ๋˜๊ณ . --> ์ด๊ฑธ ๊ณ„์† ๋ฐ˜๋ณตํ•จ.

 

 

๐Ÿ“๋ฐฉ๋ฒ• 3 : useRef ์‚ฌ์šฉํ•ด๋ณด๊ธฐ.

  • ์ž„ํฌํŠธ๋Š” ??
    • import { useRef } from "react"; 
  • ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ??
    • const count = useRef(0); // { current : 0 } // ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋ณด๊ด€๋จ.
  • ๊บผ๋‚ด์„œ ์“ฐ๋ ค๋ฉด ??
    • count.current = count.current +1;
    • ๊ฐ™์€ ๊ฐ’์„ ์ฐ๋Š” ๊ฑด๋ฐ, ์™œ html์— ์ถœ๋ ฅ๋˜๋Š” ๊ฐ’๊ณผ ์ฝ˜์†”์˜ ๊ฐ’์ด ๋‹ค๋ฅธ๊ฑฐ์ง€???
      • useRef๋Š” ๊ฐ’์ด ๋ณ€ํ•ด๋„ ๋ Œ๋”๋ง ํ•˜์ง€ ์•Š์•„. 
        ์ด ๋ง์ธ ์ฆ‰, ์ตœ์ดˆ์— ์ด๋ฏธ ๋ Œ๋”๋ง ๋์„ ๋•Œ์— count์— ๊ฐ’์ด +1 ๋˜์—ˆ๊ฒ ์ง€๋งŒ. 
        ๊ทธ๋ž˜์„œ, ์ฝ˜์†”์—์„œ๋Š” ์ฐํ˜”์ง€๋งŒ, html ์ƒ์—์„œ ๋ฐ˜์˜์ด ๋˜์ง€๋Š” ์•Š์€ ๊ฒƒ์ž„. 

 

 

๐Ÿ“ ํ’€์ฝ”๋“œ

import { useState, useEffect, useRef } from "react";
import { createRoot } from "react-dom/client";

function App() {
    const [inputValue, setInputValue] = useState("");
    const [renderValue, setRenderValue] = useState(0);
    let cnt = 0;
    const count = useRef(0); // { current : 0 } // ํ˜•ํƒœ๋กœ ๋ณด๊ด€๋จ.

    useEffect( () => {
        // ๋ฐฉ๋ฒ• 1 : useState ์‚ฌ์šฉํ•ด๋ณธ๋‹ค๋ฉด?
        // setRenderValue(renderValue + 1);
        // console.log(renderValue); // ๋ฌดํ•œ ๋ฃจํ”„ ๋Œ์Œ.
        /*  ์˜์กด์„ฑ ๋ฐฐ์—ด์„ ์ƒ๋žตํ•˜๊ฒŒ ๋˜๋ฉด, 
            ๋งจ ์ฒ˜์Œ์— ๋žœ๋”๋ง ๋  ๋•Œ, state ๊ฐ’์ด ๋ณ€ํ•˜๊ฒŒ ๋จ. 
            setRenderValue ๊ฐ€ ์ž‘๋™ํ•ด์„œ --> renderValue ๊ฐ’์ด ๋ณ€ํ•˜๊ฒŒ ๋˜์ง€.
            ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์‹œ ๋ Œ๋”๋ง์ด ์ž‘๋™ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, 
            ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด์„œ ์นด์šดํŠธ๊ฐ€ ๋ฌดํ•œ๋ฃจํ”„๋ฅผ ๋Œ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž„.
        */
        
        // ๋ฐฉ๋ฒ• 2 : ์ผ๋ฐ˜ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•ด๋ณธ๋‹ค๋ฉด?
        // cnt++;
        // console.log(cnt); // ๊ณ„์† 1๋งŒ ๋‚˜์˜ด.

        // ๋ฐฉ๋ฒ• 3 : useRef ์‚ฌ์šฉํ•ด๋ณธ๋‹ค๋ฉด?
        count.current = count.current +1;
        console.log( count.current );
        /*  useRef๋Š” ๊ฐ’์ด ๋ณ€ํ•ด๋„ ๋ Œ๋”๋ง ํ•˜์ง€ ์•Š์•„. 
            ์ด ๋ง์ธ ์ฆ‰, ์ตœ์ดˆ์— ์ด๋ฏธ ๋ Œ๋”๋ง ๋์„ ๋•Œ์— count์— ๊ฐ’์ด +1 ๋˜์—ˆ๊ฒ ์ง€๋งŒ. 
            ๊ทธ๋ž˜์„œ, ์ฝ˜์†”์—์„œ๋Š” ์ฐํ˜”์ง€๋งŒ, html ์ƒ์—์„œ ๋ฐ˜์˜์ด ๋˜์ง€๋Š” ์•Š์€ ๊ฒƒ์ž„. 
        */ 
    });
    
    return (
        <>
            <p>๊ฐ’์„ ์ž…๋ ฅํ•ด๋ณด์‹œ์š”.</p>
            <input type="text" value={inputValue} 
                onChange={(e) => setInputValue(e.target.value)}/>
            <p>๋ Œ๋”๋ง ํšŸ์ˆ˜ : {count.current}</p>
        </>
    );
}

createRoot(document.getElementById('react4')).render(<App />);

 

 

 

 

๐Ÿ“useRef ์˜ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ!! 

  • ํŠน์ • ํƒœ๊ทธ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ธฐ๋Šฅ๋„ ํ•  ์ˆ˜ ์žˆ์Œ. 
  • ์ง์ „ ๊ฐ’ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ๋„ ์žˆ๊ณ !! 
  • inputValue = ํ˜„์žฌ state (์ด๋ฒˆ ๋ Œ๋” ๊ฐ’)
  • a.current = ์ง์ „ ๊ฐ’ (useEffect๊ฐ€ ๋‹ค์Œ์— ์ €์žฅ)
import { useEffect, useRef, useState } from "react";
import { createRoot } from "react-dom/client";

function App() {
    const [inputValue, setInputValue] = useState("");
    const a = useRef();

    useEffect( () => {
        a.current = inputValue;
    }, [inputValue]);

    const focusInput = () => {
        a.current.focus();
    };

    return (
        <>
            <input type="text" value={inputValue} ref={a} onChange={ (e) => setInputValue(e.target.value) } />
            <h2>ํ˜„์žฌ ๊ฐ’ : {inputValue}</h2>
            <h2>์ง์ „ ๊ฐ’ : {a.current}</h2>

            <button onClick={focusInput}>input ๋ฐ•์Šค์— ํฌ์ปค์Šค ์ฃผ๊ธฐ</button>
        </>
    );
}

createRoot(document.getElementById('react4')).render(<App />);
  •  

 


 

 

์ถœ์ฒ˜ : 

[์™•์ดˆ๋ณด ํ™ˆํŽ˜์ด์ง€๋งŒ๋“ค๊ธฐ ๋ฆฌ์•กํŠธ ๊ธฐ์ดˆ]

https://www.youtube.com/watch?v=-jutG7mmFK0&list=PLTb3qGCzYjS2AliTIbz9eAGjZSdEQa3m1&index=39

 

 


๊ฐœ๋ฐœ ๊ณต๋ถ€๋ฅผ ์œ„ํ•œ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค. 

์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”! 

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

728x90

'front > react' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[react] React Hook 4 (useCallback, useMemo)  (0) 2026.01.07
[react] React Hook 3 (useReducer)  (0) 2026.01.07
[react] React Hooks 1 (useState, useEffect)  (0) 2026.01.04
[react] React Transition (useTransition)  (0) 2026.01.04
[react] React Router (BrowserRouter, Routes, Route, Link, Outlet, useParams)  (0) 2026.01.04