ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [React] useContext ์‚ฌ์šฉ๋ฒ• _(๋ฆฌ์•กํŠธ Hooks)
    JavaScript/React 2023. 6. 21. 13:29
    728x90
    ๋ฐ˜์‘ํ˜•

     

    ๐Ÿง React Hooks์˜ useContext๋ฅผ ์•Œ์•„๋ณด์ž

     
    props๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ€๋” ๋น„ํšจ์œจ์ ์ด๋ผ๊ณ  ๋Š๋‚„ ๋•Œ๊ฐ€ ์žˆ์ฃ . ๊ทธ๋Ÿด ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ context์ž…๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ…์€ react ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ useContext Hook์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ  ์‚ฌ์šฉํ•ด ๋ณผ๊ฒŒ์šฉ
     
     
     


    ๐ŸŠ useContext๋ž€ ๋ฌด์—‡์ผ๊นŒ?

    useContext๋ž€ ๋ฆฌ์•กํŠธ hook์˜ ํ•˜๋‚˜๋กœ, ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒ์„ฑํ•œ context๋ฅผ ์ฝ๊ณ (read), ๊ตฌ๋…(subscribe)ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.
     

    ๐Ÿคท๐Ÿปโ€โ™€๏ธ context? ๊ทธ๊ฒŒ ๋ญ”๋ฐ์š”

    context๋ž€ ๋ง์ด ์™€๋‹ฟ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. context๋ฅผ ์ง์—ญํ•˜๋ฉด ๋ฌธ๋งฅ, ๋ฐฐ๊ฒฝ, ์ƒํ™ฉ์ด๋ผ๋Š” ๋œป์ธ๋ฐ, props์˜ ๋Œ€์•ˆ์œผ๋กœ ๋‚˜์˜จ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๊ฐ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š์•„๋„ ๋˜๋Š” props๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค. 
     
     
    ์˜ˆ๋ฅผ ๋“ค์–ด prop์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด prop drilling์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋งˆ๋ จ์ธ๋ฐ,

    ์ด๋Ÿฌํ•œ ํ˜„์ƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด context์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
     
    context๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋ถ€๋ชจ์—์„œ ์›ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์— ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     
     


     

    ๐ŸŠ context ์‚ฌ์šฉ๋ฒ•

    ๐Ÿ“Œ context ์ƒ์„ฑํ•˜๊ธฐ

    const MyContext = createContext(null);

    context๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” createContext๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
     
     

    ๐Ÿ“Œ context๋ฅผ ์‚ฌ์šฉํ•  ๋ฒ”์œ„ ์„ค์ •ํ•˜๊ธฐ

    <MyContext.Provider value={some}> 
    </MyContext.Provider>

    context๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์€ ์Šค์ฝ”ํ”„๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ƒ์œ„๋ ˆ๋ฒจ์—์„œ < ์ƒ์„ฑํ•œ ์ปจํ…์ŠคํŠธ๋ช….Provider> ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ์‹ธ์ฃผ๊ณ  value๋กœ context ๊ฐ’ some์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. (์—ฌ๊ธฐ์„œ some์€ ์ž„์˜๋กœ ์ง€์ •ํ•œ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.)
     
     

    ๐Ÿ“Œ context ์‚ฌ์šฉํ•˜๊ธฐ

    const some = useContext(MyContext);

    ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ context๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” useContext๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ด„ํ˜ธ ์•ˆ์— ์ธ์ž๋กœ ์ƒ์„ฑํ•œ context๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  ๋ณ€์ˆ˜์— ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
     
     
     


     

    ๐ŸŠ ์ด์ œ ์ง์ ‘ ์‚ฌ์šฉํ•ด๋ณด์ž

    context๋ฅผ ์ด์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์€ form์˜ ํ…Œ๋งˆ๋ฅผ ๊ด€๋ฆฌํ•ด๋ณผ๊ฒŒ์š”. ํ…Œ๋งˆ๊ฐ€ dark์ธ์ง€ light์ธ์ง€ context๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์ ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
     

     
     

    ๐Ÿ”— createContext ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒˆ context ์ƒ์„ฑํ•˜๊ธฐ

    const ThemeContext = createContext('light');

    ThemeContext๋ผ๊ณ  ํ•˜๋Š” ๋ฌธ์ž์—ด ํ…Œ๋งˆ ์„ค์ •์„ ๋‹ด์„ context๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    ๋””ํดํŠธ๊ฐ’์€ light๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
     
     
    ๐Ÿ”— ์ปจํ…์ŠคํŠธ๋ช….Provider ์ปดํฌ๋„ŒํŠธ๋กœ ์‚ฌ์šฉํ•  ๊ณณ์„ ์ง€์ •ํ•˜๊ธฐ

    export default function MyApp() {
      const [theme, setTheme] = useState('light');
      return (
        <>
          <ThemeContext.Provider value={theme}> // value๋กœ ํ…Œ๋งˆ๊ฐ’ ์„ค์ •
            <Form />
          </ThemeContext.Provider>
          
          // ๋‹คํฌ๋ชจ๋“œ ๋ผ์ดํŠธ๋ชจ๋“œ ์ „ํ™˜ํ•˜๋Š” ๋ฒ„ํŠผ
          <Button onClick={() => {
            setTheme(theme === 'dark' ? 'light' : 'dark');
          }}>
            Toggle theme
          </Button>
        </>
      )
    }

     
     
    ๐Ÿ”— useContext(์‚ฌ์šฉํ•  context) ๋กœ ๋ถ€๋ชจ์˜ context ๋ฌผ๋ ค๋ฐ›๊ธฐ

    function Form({ children }) {
      return (
        <Panel title="Welcome">
          <Button>Sign up</Button>
          <Button>Log in</Button>
        </Panel>
      );
    }
    
    function Panel({ title, children }) {
      const theme = useContext(ThemeContext); // useContext ๋ถ€๋ชจ context ๊ฐ€์ ธ์˜ค๊ธฐ
      const className = 'panel-' + theme;
      return (
        <section className={className}>
          <h1>{title}</h1>
          {children}
        </section>
      )
    }
    
    function Button({ children, onClick }) {
      const theme = useContext(ThemeContext); // useContext ๋ถ€๋ชจ context ๊ฐ€์ ธ์˜ค๊ธฐ
      const className = 'button-' + theme;
      return (
        <button className={className} onClick={onClick}>
          {children}
        </button>
      );
    }

     
    ๐Ÿ”— ๊ฒฐ๊ณผ

    Toggle theme๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋‹คํฌ๋ชจ๋“œ์™€ ๋ผ์ดํŠธ๋ชจ๋“œ๋ฅผ ํ† ๊ธ€ ํ˜•์‹์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
     
     


    ๐ŸŠ ์š”์•ฝ

    useContext hook์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”! prop์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ์—†์—ˆ๋‹ค๋ฉด ๊ธ€์„ ์ฝ๋Š” ๋ฐ ์–ด๋ ค์›€์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์—๋Š” prop์— ๋Œ€ํ•ด์„œ ํฌ์ŠคํŒ…ํ•˜๊ณ  ๋งํฌ๋ฅผ ๊ฑธ์–ด๋‘ฌ์•ผ๊ฒ ์–ด์š”! ์•„๋ฌดํŠผ context๋Š” prop์˜ ๋Œ€์•ˆ์œผ๋กœ ๋‚˜์™”๋‹ค๋Š” ๊ฒƒ๊ณผ ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ์— ์ข‹์€ ์ ์ด๋ผ๋Š” ๊ฒƒ๋งŒ ๊ธฐ์–ตํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! 
     
    ๋‹ค์Œ์—๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์•กํŠธ ํ›…์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!
     
     
     
    ์ฐธ๊ณ ๋ฌธ์„œ
     
    https://react.dev/reference/react/useContext#usage

    useContext โ€“ React

    The library for web and native user interfaces

    react.dev

    https://react.dev/learn/passing-data-deeply-with-context

    Passing Data Deeply with Context โ€“ React

    The library for web and native user interfaces

    react.dev

     

    LIST
Designed by Tistory.