[React]07. Context API ์‚ฌ์šฉ

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

React

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

์˜ค๋Š˜์€ Context API๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด๋ณผ๊ฒŒ์š”!

๐Ÿ’š Context API

์ „์—ญ์ ์ธ ๋ฐ์ดํ„ฐ ๊ณต์œ ๋ฅผ ์œ„ํ•œ ๋ฐฉ๋ฒ•
๋กœ๊ทธ์ธ ๋˜๋Š” ์›น ์‚ฌ์ดํŠธ ๋‚ด ์‚ฌ์šฉ์ž ์„ค์ •(ํ…Œ๋งˆ๋‚˜ ์–ธ์–ด๋“ฑ)์— ์‚ฌ์šฉ
redux, react-router, styled-components ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋‘ Context API ๊ธฐ๋ฐ˜ ๊ตฌํ˜„

Context API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ์˜ ์žฌ์‚ฌ์šฉ์ด ์–ด๋ ค์›€์œผ๋กœ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•  ๋•Œ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’› Context API ํ•„์š”ํ•œ ์ด์œ 

์ปดํฌ๋„ŒํŠธ๋Š” props๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š”๋ฐ์š”. props๋ฅผ ์ „๋‹ฌํ•ด์•ผํ•  ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งŽ์•„์ง€๋ฉด ์ด ๊ณผ์ •์€ ๋งค์šฐ ๋ฒˆ๊ฑฐ๋กญ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๋‚ฎ์•„์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” redux์™€ mobx์™€ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํ•˜๋Š”๋ฐ์š”! ContextAPI๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ๋„ ๊ธ€๋กœ๋ฒŒํ•œ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’š Context API 3๊ฐ€์ง€ ์ฃผ์š” ๊ฐœ๋…

Context API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— 3๊ฐ€์ง€ ์ฃผ์š” ๊ฐœ๋…๋ถ€ํ„ฐ ์ •๋ฆฌํ•ด๋ณผ๊ป˜์š”!

๐Ÿ’› React.createContext(defaultValue)

  • context ๊ฐ์ฒด ์ƒ์„ฑ์‹œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜
  • createContextํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ Provider์™€ Consumer์ปดํฌ๋„ŒํŠธ ๋ฐ˜ํ™˜
  • defaultValue๋Š” ์ดˆ๊ธฐ๊ฐ’ ์˜๋ฏธ(Provider๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ ์ „๋‹ฌ๋  ๊ฐ’)

๐Ÿ’› Context.Provider

  • context์˜ value๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์—ญํ• 
  • Provider๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ๐Ÿ“Œvalue ๐Ÿ“Œ๋ช…์‹œ ํ•„์š”
  • ์ „๋‹ฌ๋ฐ›๋Š” ์ปดํฌ๋„ŒํŠธ ์ˆ˜ ์ œํ•œ ์—†์Œ

๐Ÿ’›Context.Consumer

  • context๋ณ€ํ™”๋ฅผ ๊ตฌ๋…ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ
  • ์„ค์ •ํ•œ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์‚ฌ์šฉ

๐Ÿ’š Context API ์‚ฌ์šฉ

context API ์‚ฌ์šฉ ์˜ˆ์ œ๋Š” ์•„๋ž˜ ์‚ฌ์ดํŠธ๋ฅผ ๋ณด๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋”๋ถ:๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ 

๋ฒ„ํŠผ์˜ ์ƒ‰์ƒ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” context๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฒ„ํŠผ์˜ ์ƒ‰์ƒ์„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•ด๋ณด๋„๋ก ํ• ๊ฒŒ์š”!

๋จผ์ €, context๋ฅผ ๋งŒ๋“ค์–ด์ค„๊บผ์—์š”!

import { createContext } from "react";

// createContext()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ColorContext ์ƒ์„ฑ. ์ดˆ๊ธฐ๊ฐ’์€ skyblue์„ค์ •
const ColorContext = createContext({ color: "skyblue" });

export default ColorContext;

๊ทธ๋ฆฌ๊ณ  ๋งŒ๋“ค์–ด์ค€ColorContext๋ฅผ ์‚ฌ์šฉํ•  button์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
Consumer๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ ์šฉํ•œ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ์š”!

import React from "react";
import ColorContext from "../contexts/color";

const ColorButton = () => {
  return (
    <ColorContext.Consumer>
      {(value) => (
        <button
          style={{
            width: "150px",
            height: "50px",
            background: value.color
          }}
        >
          Button!
        </button>
      )}
    </ColorContext.Consumer>
  );
};

export default ColorButton;

์•„๋ž˜์ฒ˜๋Ÿผ ColorContext์—์„œ ์„ค์ •ํ•ด์ค€ color๊ฐ’์„ ๊ฐ€์ ธ์™€ ๋ฒ„ํŠผ์˜ ๋ฐฐ๊ฒฝ์ƒ‰์œผ๋กœ ์ ์šฉํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ์—๋Š” Provider๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ value๋ฅผ ๋ณ€๊ฒฝํ•ด ์ฃผ๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

export default function App() {
  return (
      <ColorContext.Provider value={{color:"orange"}}>
        <div>
          <h2>Context API ์‚ฌ์šฉํ•ด๋ณด๊ธฐ!</h2>
          <ColorButton />
        </div>
      </ColorContext.Provider>
  );
}
profile
๊ฐœ๋ฐœ ๊ธฐ๋ก๐Ÿ’ป

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