Home ๐Ÿ“ธ 2022-07-28
Post
Cancel

๐Ÿ“ธ 2022-07-28

๐Ÿ”ฅ ์˜ค๋Š˜ ์–ด๋””์— ๋„์ „ํ•ด๋ณด์…จ๊ณ , ๋ฌด์—‡์„ ๋ฐฐ์šฐ์…จ๋‚˜์š”?

๐ŸŒŠ ํ•™์Šตํ•˜์‹œ๋ฉด์„œ ๊ถ๊ธˆํ•˜์‹  ๋ถ€๋ถ„์ด๋‚˜, ์–ด๋ ค์› ๋˜ ์ ์€ ๋ญ˜๊นŒ์š”?

  • ์ฃผ์˜์‚ฌํ•ญ์— Provider์˜ value prop์ด ๋ฐ”๋€Œ๋ฉด ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋ฏ€๋กœ Provider๊ฐ€ ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ํ•˜์œ„์˜ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ชจ๋‘ ๋ฆฌ๋ Œ๋”๋ง๋œ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” state๋กœ ๋Œ์–ด์˜ฌ๋ฆฌ๊ณ  ๊ทธ state ๊ฐ’์„ value prop์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค.

    1
    2
    3
    4
    
    Object.is({ a: "1" }, { a: "1" }); //false
    
    const obj = { a: "1" };
    Object.is(obj, obj); // true
    

    ๊ฐ์ฒด ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋Š” ๋™์ผํ•˜๋”๋ผ๋„ ๊ฐ๊ฐ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์ธ์‹ํ•œ๋‹ค.

    Provider์˜ value prop์—๋„ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์ธ์‹๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•œ ํ›„์— ๊ทธ ๊ฐ’์„ value prop์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๐ŸŒŸ ๋‚ด์ผ์€ ์–ด๋–ป๊ฒŒ ํ•ด๋ณด๊ณ  ์‹ถ์œผ์„ธ์š”?

  • ์œ ๋ฐ๋ฏธ ๋ฆฌ์•กํŠธ ๊ฐ•์˜ 122 ~ 128๊นŒ์ง€ ์ˆ˜๊ฐ•ํ•˜๊ธฐ (context ์‹ฌํ™”)

React์˜ Context

  • context๋Š” ๋ฆฌ์•กํŠธ ๋‚ด๋ถ€์—์„œ ์ „์—ญ์ ์œผ๋กœ state๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ €์žฅ์†Œ ๊ฐœ๋…์ด๋‹ค.
  • context๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‹จ๊ณ„๋งˆ๋‹ค ์ผ์ผ์ด props๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๊ณ (=prop chain์„ ๊ตฌ์ถ•ํ•˜์ง€ ์•Š๊ณ ๋„) ์ปดํฌ๋„ŒํŠธ ํŠธ๋ฆฌ ์ „์ฒด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • React.createContext๋กœ Context ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , Context.Provider๋กœ context๋ฅผ ์‚ฌ์šฉํ•  ์ปดํฌ๋„ŒํŠธ์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•˜์—ฌ ๋ฆฌ์•กํŠธ์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ณ  ๋‚ด๋ถ€ ์ปดํฌ๋„ŒํŠธ๋“ค์—๊ฒŒ๋Š” context์˜ ๋ณ€ํ™”๋ฅผ ์•Œ๋ฆฐ๋‹ค.
  • Context.Provider ๋‚ด๋ถ€์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•ด๋‹น context์˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Context.Consumer ๋˜๋Š” useContext๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  1. React.createContext

    1
    
    const MyContext = React.createContext(defaultValue);
    

    ์ „์—ญ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ๋ฅผ defaultValue ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค.

  2. Context.Provider

    1
    2
    3
    4
    5
    6
    7
    
    const App = () => {
      return (
        <MyContext.Provider value={providedValue}>
          <Toolbar />
        </MyContext.Provider>
      );
    };
    

    Provider ์ปดํฌ๋„ŒํŠธ์—๋Š” value๋ผ๋Š” prop์„ ์ „๋‹ฌํ•ด์ฃผ๋Š”๋ฐ ์ด ๊ฐ’์ด ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋“ค์—๊ฒŒ ์ „๋‹ฌ๋˜๊ณ  ์ด value prop์ด ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค ๋ฆฌ๋ Œ๋”๋ง๋œ๋‹ค.

    ์œ„ ์ฝ”๋“œ์—์„œ Toolbar ์ปดํฌ๋„ŒํŠธ๋Š” MyContext.Provider ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์— ์œ„์น˜ํ•˜๋ฏ€๋กœ MyContext.Provider์— ์ „๋‹ฌ๋˜๋Š” value prop์ด ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค ๋ฆฌ๋ Œ๋”๋ง๋œ๋‹ค.

    โš ๏ธ React.createContext์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•œ defaultValue๋Š” Provider๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์„ ๋•Œ๋งŒ ์“ฐ์ด๋Š” ๊ฐ’์ด๋‹ค.

  3. Context.Consumer

    1
    2
    3
    4
    5
    6
    7
    
    const Toolbar = () => {
        return <MyContext.Consumer>
        {(ctx) => {
         return (<ul>{...}</ul>)
        }}
        </MyContext.Consumer>
    }
    

    Consumer ์ปดํฌ๋„ŒํŠธ์˜ ์ž์‹์€ ํ•จ์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” context์˜ ํ˜„์žฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. ์—ฌ๊ธฐ์„œ ctx๋Š” providedValue๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

  4. useContext

    1
    2
    3
    4
    
    const Toolbar = () => {
        const ctx = useContext(MyContext);
        return <ul>{...}</ul>
    }
    

    MyContext.Consumer์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ MyContext.Provider ๋‚ด๋ถ€์—์„œ useContext ํ›…์œผ๋กœ context์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

This post is licensed under CC BY 4.0 by the author.

๐Ÿ“ธ 2022-07-26

React์˜ Ref