๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒณ React

[React] ContextHook(useContext) ์ •์˜์™€ ์‚ฌ์šฉ์˜ˆ์‹œ

by ._.sori 2026. 1. 12.

 

 

๋ฆฌ์•กํŠธ์˜ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค.
๊ทธ๋Ÿฌ๋ฉด์„œ ์ž์—ฐ์Šค๋ ˆ Hook์— ๋Œ€ํ•ด ์•Œ๊ฒŒ๋˜์—ˆ๊ณ ,
React ์ฝ”๋“œ์˜ ๋Œ€๋ถ€๋ถ„์€ Hook์œผ๋กœ ์ด๋ค„์ง„ ๊ฑธ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.
Hook์ด ๋ฌด์—‡์ธ์ง€, ์ž์„ธํ•˜๊ฒŒ ์•Œ์•„๋ณด์ž.

 

 

 

 

1. React Hook์˜ ๋“ฑ์žฅ ๋ฐฐ๊ฒฝ

  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜๋‰œ๋‹ค.
  • ๋ฆฌ์•กํŠธ ์ดˆ๊ธฐ์—๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋‚˜, ๊ฐ’์˜ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌ(state) ํ˜น์€ ์ƒ๋ช…์ฃผ๊ธฐ(์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ์‚ฌ๋ผ์ง€๋Š” ๊ณผ์ •)๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•  ๋•Œ๋งŒ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ๋Š” ์ฝ”๋“œ์˜ ๊ตฌ์„ฑ์ด ์–ด๋ ต๊ณ  ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋–จ์–ด์ง„๋‹ค. ์ฝ”๋“œ ์ตœ์ ํ™”๋„ ์–ด๋ ต๊ณ  ์ตœ์‹  ๊ธฐ์ˆ  ์‚ฌ์šฉ๋„ ํšจ๊ณผ์ ์ด์ง€ ์•Š๋‹ค.
  • ๊ทธ๋ž˜์„œ ์ด๋Ÿฐ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๋‹จ์ ์„ ๋ณด์•ˆํ•˜๋ฉด์„œ, ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋“ฑ์žฅํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ React Hook(๋ฆฌ์•กํŠธ ํ›…)์ด๋‹ค.

 

 


 

 

 

2. React Hook

  • ๋ฆฌ์•กํŠธ v16.8์— ์ƒˆ๋กœ ๋„์ž…๋œ ๊ธฐ๋Šฅ์œผ๋กœ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ช‡๊ฐ€์ง€ ๊ธฐ์ˆ ๋“ค์„ ์ผ์ปซ๋Š”๋‹ค.
  • ๋ฆฌ์•กํŠธ ํ›…์€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.
  • ์ฃผ๋กœ ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด useEffect, useState ๋“ฑ์ด ์žˆ๋‹ค.

 

 

 


 

 

 

3. ContextHook

  • Context๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ Props๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ ๋„ ๋ฉ€๋ฆฌ ์žˆ๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

  • useContext
    a) 
    useContext๋Š” ์ปดํฌ๋„ŒํŠธ์—์„œ Context๋ฅผ ์ฝ๊ณ  ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋Š” React Hook์ด๋‹ค.
    b) useContext๋Š” ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š”์ง€์— ๋Œ€ํ•œ SomeContext(ThemeContext)๊ฐ€ ํ•„์š”ํ•˜๋‹ค. 
    c) ์ด๋•Œ ์ค‘์š”ํ•œ ๊ฒƒ์€ SomeContext(ThemeContext)๋Š” ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์ง€ ์•Š๋‹ค. ์ปดํฌ๋„ŒํŠธ์—์„œ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ์ •๋ณด์˜ ์ข…๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    d) ํŠธ๋ฆฌ์—์„œ ํ˜ธ์ถœํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ์ƒ์œ„์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด SomeContext(ThemeContext)์— ์ „๋‹ฌ๋œ ๊ฐ’์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค. (๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋ฌด์กฐ๊ฑด ์œ„์— SomeContext๋ฅผ ์ ์–ด๋‘ฌ์•ผํ•œ๋‹ค. ํ•˜์œ„์— ์ ์œผ๋ฉด ์•ˆ๋œ๋‹ค.)
    e) Context๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด React๋Š” ์ž๋™์œผ๋กœ ํ•ด๋‹น Context๋ฅผ ์ฝ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค์‹œ ๋ Œ๋”๋งํ•œ๋‹ค.
import { createContext, useContext, useState } from 'react';

// 1.
// Context๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
// ์—ฌ๊ธด ํ…Œ๋งˆ ์ปจํ…์ŠคํŠธ๋กœ ์„ค์ •๋œ ๊ฐ’์„ Provider ์ „๋‹ฌํ•œ๋‹ค.
const ThemeContext = createContext(null);

export default function MyApp() {
  // 2.
  // ๊ธฐ๋ณธ๊ฐ’์€ ๋ฐ๊ฒŒ ์„ค์ •๋˜์–ด์žˆ๋‹ค. useState๋กœ ๋ณ€๊ฒฝ์„ ๊ฐ์ง€ํ•˜์—ฌ ๋ Œ๋”๋งํ•  ๊ฒƒ์ด๋‹ค.
  const [theme, setTheme] = useState('light');
  return (
    // 3.
    // ์ฒดํฌ๋ฐ•์Šค๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•˜๊ณ  ์ฒดํฌํ•  ์‹œ, ์ด๋ฒคํŠธ ๊ฐ์ฒด e๊ฐ€ ํ™•์ธํ•œ๋‹ค.
    // e.target์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ dark๋ฅผ ์ฒดํฌํ–ˆ๋Š”์ง€, light๋ฅผ ์ฒดํฌํ–ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
    // ์ฒดํฌ๋ฐ•์Šค์— ํ‘œ์‹œ๊ฐ€ ์ƒ๊ธฐ๋ฉด true -> dark, false -> light๊ฐ€ ๋œ๋‹ค.
    <ThemeContext value={theme}>
      <Form />
      <label>
        <input
          type="checkbox"
          checked={theme === 'dark'}
          onChange={(e) => {
            setTheme(e.target.checked ? 'dark' : 'light')
          }}
        />
        Use dark mode
      </label>
    </ThemeContext>
  )
}

// Form์€ ์ƒ‰์ƒ ๋ณ€๊ฒฝ์ด ์—†๋‹ค.
function Form({ children }) {
  return (
    <Panel title="Welcome">
      <Button>Sign up</Button>
      <Button>Log in</Button>
    </Panel>
  );
}

// 
function Panel({ title, children }) {
  // 4.
  // ์œ„์— ์–ด๋–ค ๋ ˆ์ด์–ด๊ฐ€ ์žˆ๋˜์ง€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๋Š”๋‹ค.
  // useContext๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ThemeContext์— ๋‹ด๊ธด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค.
  // provider๊ฐ€ ํ…Œ๋งˆ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๊ธฐ์„œ ๋‹ด์•„์ค€๋‹ค.
  const theme = useContext(ThemeContext);
  // ์—ฌ๊ธฐ์„œ className์„ ์ •ํ•ด์ง„๋‹ค. 'panel-dark' ์ด๋Ÿฐ์‹์œผ๋กœ
  const className = 'panel-' + theme;
  return (
    // ์ƒ‰์ƒ์ด ๋ณ€๊ฒฝ๋˜์–ด ๋ฆฌํ„ด๋œ๋‹ค.
    <section className={className}>
      <h1>{title}</h1>
      // ์—ด๋ฆฐ ํƒœ๊ทธ, ๋‹ซํžŒ ํƒœ๊ทธ ์‚ฌ์ด์— ๋„ฃ์€ ๋ชจ๋“  ๊ฑธ children์ด๋ผ๊ณ  ํ•œ๋‹ค.
      {children}
    </section>
  )
}

function Button({ children }) {
  const theme = useContext(ThemeContext);
  const className = 'button-' + theme;
  return (
    <button className={className}>
      {children}
    </button>
  );
}

 

 

Q. SomeContext๋ฅผ ๋น„์œ ๋กœ ์ ์–ด๋ณด์ž๋ฉด?

A. ์ฒ˜์Œ์—” ์ƒ์ž๊ฐ™์ด ์ •๋ณด๋ฅผ ๋‹ด์•„์„œ ์ค€๋‹ค๊ณ  ์ดํ•ดํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฆฌ์•กํŠธ ํŽ˜์ด์ง€์— ๋ณด๋ฉด ์ •๋ณด๋ฅผ ๋‹ด์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํ†ต๋กœ๊ฐ™์€ ๊ฐœ๋…์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค. ํ…Œ๋งˆ ์ƒ‰์ƒ์ด ๋ฐ”๋€Œ๋ฉด ๋ฐ”๋€Œ์—ˆ๋‹ค๋Š” ๋ฐ”๋€ ์ •๋ณด๋ฅผ Provider๋กœ ์ „๋‹ฌ๋งŒ ํ•˜๋Š” ๋ฐฐ๊ด€, ์ „๊นƒ์ค„ ๊ฐ™์€ ์—ญํ• ์ธ ๋“ฏ ํ•˜๋‹ค.

 

 

 

 

 

 


 

 

- react hooks

 

๋‚ด์žฅ React Hook – React

The library for web and native user interfaces

ko.react.dev

 

 

 

- useContext

 

useContext – React

The library for web and native user interfaces

ko.react.dev