[shop-project] Context API

kirin.logยท2021๋…„ 4์›” 28์ผ
0
post-custom-banner

๐ŸŒˆ Context API ๋ž€?

  • ๋ฆฌ์•กํŠธ ๋ฌธ๋ฒ• ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
  • ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ์ค‘์ฒฉ๋  ๊ฒฝ์šฐ, state โžก props โžก props โžก props ์ด๋Ÿฐ์‹์œผ๋กœ props๊ฐ€ ๊ณ„์†ํ•ด์„œ ์ค‘์ฒฉ๋˜์–ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ Context API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด props ์ „์†ก ์—†์ด๋„ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋“ค๋ผ๋ฆฌ state๊ฐ’๋“ค์„ ๋˜‘๊ฐ™์ด ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Context API๋Š” ํฌ๊ฒŒ ์ „์—ญ ์ƒํƒœ๊ฐ€ ์ €์žฅ๋˜๋Š” context, ์ „์—ญ ์ƒํƒœ๋ฅผ ์ œ๊ณตํ•˜๋Š” Provider, ๊ทธ๋ฆฌ๊ณ  ์ „์—ญ ์ƒํƒœ๋ฅผ ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” Consumer๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ๋‹ค.

โ— ์ค‘์ฒฉ๋œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ช‡ ๊ฐœ ์—†์œผ๋ฉด props๊ฐ€ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.

๐ŸŽ Context API ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

1) import ํ•ด์ฃผ๊ธฐ โžก hook์„ ์ง€์ •ํ•ด์ค€๋‹ค
import React, { useContext } from 'react';

2) Context ๋งŒ๋“ค๊ธฐ โžก import์™€ function์„ ์–ธ ์‚ฌ์ด์— React.createContext()๋ฅผ ์ €์žฅํ•˜์—ฌ exportํ•ด์ค€๋‹ค
export let ๋ฒ”์œ„context = React.createContext();
โ— ๊ฐ™์€ ๋ณ€์ˆ˜๊ฐ’์„ ๊ณต์œ ํ•  ๋ฒ”์œ„๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒƒ.

3) Provider ๋งŒ๋“ค๊ธฐ โžก ๊ฐ™์€ ๊ฐ’์„ ๊ณต์œ ํ•  HTML์„ Provider๋กœ ๊ฐ์‹ธ์ฃผ๊ธฐ
<๋ฒ”์œ„context.Provider> ๊ฐ’์„ ๊ณต์œ ํ•˜๋ ค๋Š” HTML ํƒœ๊ทธ๋“ค </๋ฒ”์œ„context.Provider>
โ— ๊ฐ™์€ ๋ณ€์ˆ˜๊ฐ’์„ ๊ณต์œ ํ•  ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์‹ธ์ฃผ๋Š” ๊ฒƒ.

4) value ์ง€์ • โžก Provider ํƒœ๊ทธ ์•ˆ์— ๊ณต์œ ๋ฅผ ์›ํ•˜๋Š” ๊ฐ’์„ ์ง€์ •ํ•ด์ค€๋‹ค
<๋ฒ”์œ„context.Provider value={๊ณต์œ ํ•˜๊ธธ ์›ํ•˜๋Š” state๊ฐ’}>
โ— ๊ณต์œ ํ•˜๊ณ  ์‹ถ์€ state๊ฐ’์„ ๋‹ด๊ธฐ ์œ„ํ•ด ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ.

5) (๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ)useContext(๋ฒ”์œ„์ด๋ฆ„)๋กœ ๊ณต์œ ๋œ ๊ฐ’ ์‚ฌ์šฉํ•˜๊ธฐ
let ๋ณ€์ˆ˜์ง€์ • = useContext(๋ฒ”์œ„context)
โ— useContext()์•ˆ์— ์ƒ์œ„์—์„œ ๊ณต์œ  ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•œ ๋ณ€์ˆ˜๋ช…์„ ๋‹ด๋Š”๋‹ค.


๐Ÿป useContext๋ฅผ ์‚ฌ์šฉํ•ด์„œ state๊ฐ’ ๊ณต์œ  ์˜ˆ์‹œ

// App.js
import React, { useState, useContext } from 'react';  // import ํ•ด์ค€๋‹ค
import Product from './component/Product';

export let ์žฌ๊ณ context = React.createContext();   // ๋ณ€์ˆ˜์— ๋‹ด์•„์„œ export ํ•ด์ค€๋‹ค

function App() {

  let [์žฌ๊ณ , ์žฌ๊ณ ๋ณ€๊ฒฝํ•จ์ˆ˜] = useState([10, 11, 12])

  return (
    <์žฌ๊ณ context.Provider value={์žฌ๊ณ }>  // ์žฌ๊ณ state ๊ณต์œ ๋ฅผ ์›ํ•˜๋Š” html ๋ฒ”์œ„๋ฅผ ๊ฐ์‹ธ์ค€๋‹ค
      <div className="row">
        {
           shoes.map((a,i) => {
              return ( <Product shoes={shoes[i] key=[i]} /> )
           })
        }
      </div>
    </์žฌ๊ณ context.Provider>
  )
}



// Product.js
import React, { useState} from 'react'; 
import Lest from './Lest.js';

function Product(props) {

  return (
    <div>
        <h4> {props.shoes.title} </h4>
        <p> {props.shoes.content} & {props.shoes.price} </p>

        <Lest />

    </div>
  )
}
โ— ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ(App.js)์—์„œ ์ค‘๊ฐ„ ์ปดํฌ๋„ŒํŠธ(Product.js)๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ(Lest.js)๋กœ 
๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ React.createContext๋กœ ๋ฒ”์œ„๋ฅผ ์„ค์ •ํ•ด ๋†“์œผ๋ฉด,
ํ•ด๋‹น ๋ฒ”์œ„๋ฅผ ๊ณต์œ ํ•  ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ”๋กœ ๊ฐ€์„œ ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ๋‹ค.



// Lest.js
import React, { useState, useContext } from 'react'; 
import { ์žฌ๊ณ context } from './App.js';   // ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ importํ•ด์˜จ๋‹ค

function Lest() {

  let lest = useContext(์žฌ๊ณ context);   // ๊ณต์œ  ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•œ ๋ณ€์ˆ˜๋ช…(์žฌ๊ณ context)์„ ๋‹ด์•„์„œ ๊ฐ€์ ธ๋‹ค์“ด๋‹ค

  return (
    <p> ์žฌ๊ณ : {lest}  </p>
  )
}
profile
boma91@gmail.com
post-custom-banner

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