๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
React

[React] Context API์™€ ์˜์กด์„ฑ ์ฃผ์ž…

by LasBe 2023. 5. 8.
๋ฐ˜์‘ํ˜•

๐Ÿ“’ Context API๋ž€ ๋ฌด์—‡์ธ๊ฐ€?


๋ฆฌ์•กํŠธ ๊ณต์‹๋ฌธ์„œ์—์„œ๋Š” Context๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†Œ๊ฐœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

context๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‹จ๊ณ„๋งˆ๋‹ค ์ผ์ผ์ด props๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๊ณ ๋„
์ปดํฌ๋„ŒํŠธ ํŠธ๋ฆฌ ์ „์ฒด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ผ๋ฐ˜์ ์ธ React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐ์ดํ„ฐ๋Š” ์œ„์—์„œ ์•„๋ž˜๋กœ
(์ฆ‰, ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ž์‹์—๊ฒŒ) props๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋˜์ง€๋งŒ,
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•ˆ์˜ ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ๋“ค์— ์ „ํ•ด์ค˜์•ผ ํ•˜๋Š” props์˜ ๊ฒฝ์šฐ
(์˜ˆ๋ฅผ ๋“ค๋ฉด ์„ ํ˜ธ ๋กœ์ผ€์ผ, UI ํ…Œ๋งˆ) ์ด ๊ณผ์ •์ด ๋ฒˆ๊ฑฐ๋กœ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

context๋ฅผ ์ด์šฉํ•˜๋ฉด, ํŠธ๋ฆฌ ๋‹จ๊ณ„๋งˆ๋‹ค ๋ช…์‹œ์ ์œผ๋กœ props๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์•„๋„
๋งŽ์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ด๋Ÿฌํ•œ ๊ฐ’์„ ๊ณต์œ ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌ์•กํŠธ์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ ๊ฐ„ prop drilling์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด context๋ฅผ ๋‚ด๋†“์•˜์Šต๋‹ˆ๋‹ค.

 

context๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์™€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋›ฐ์–ด๋„˜์–ด ๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์‚ฌ์šฉ๋ฒ• ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ Context Api ์‚ฌ์šฉ๋ฒ•

๐Ÿ”Ž React.createContext

const NewContext = React.createContext(defaultValue);
// ํƒ€์ž… ์ง€์ •๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
const NewContext2 = React.createContext<{value:string}>({value:''});

์šฐ์„  React.createContext๋ฅผ ํ†ตํ•ด context ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

defaultValue๋Š” ํŠธ๋ฆฌ ์•ˆ์—์„œ ์ ์ ˆํ•œ Provider๋ฅผ ์ฐพ์ง€ ๋ชปํ•  ๋•Œ๋งŒ ์“ฐ์ด๋Š” ๊ธฐ๋ณธ๊ฐ’์ž…๋‹ˆ๋‹ค.

 

Provider์— value๊ฐ€ undefined์ด์—ฌ๋„ defaultValue๋ฅผ ์ œ๊ณตํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”Ž React.Provider

const DisplayContext = React.createContext<{mode:'light'|'dark'}>({mode:'light'});
...
return (
    <DisplayContext.Provider value={mode:'light'}>
        <App />
    </DisplayContextProvider>
)

Provider๋Š” ๋ฐ›์€ value ๊ฐ’์„ ํ•˜์œ„์— ์œ„์น˜ํ•œ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

 

Provider์•ˆ์— Provider๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋Ÿด ๊ฒฝ์šฐ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ถ€๋ชจ Provider์˜ ๊ฐ’์ด ์šฐ์„ ๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  context๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋Š” ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋“ค์€ Provider์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ์ „๋ถ€ ๋ Œ๋”๋ง๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๊ธฐ์— ํ•œ context ์•ˆ์— ์—ฐ๊ด€์„ฑ์ด ์—†๋Š” ๊ฐ’๋“ค์„ ํ•จ๊ป˜ ๋„ฃ์„ ๊ฒฝ์šฐ ์„ฑ๋Šฅ ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
์—ฐ๊ด€์„ฑ ์žˆ๋Š” ๊ฐ’๋“ค๋ผ๋ฆฌ ๋ถ„๋ฆฌํ•˜์—ฌ ๋‹ค๋ฅธ context๋กœ ๋ถ„๋ฆฌํ•ด์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”Ž React.useContext

const App = () => {
    const { mode } = React.useContext(DisplayContext);

    return (
        <div style={{background : mode === 'light' ? 'white' : 'black'}}>
            Hello
        </div>
    )
}

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

return (
    <DisplayContext.Consumer>
        {value => (
            <div style={{background : value.mode === 'light' ? 'white' : 'black'}}>
                Hello
            </div>
        )}
    </DisplayContext.Consumer>
)

๊ธฐ์กด์—๋Š” Consumer๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ๊บผ๋‚ด์™”๋Š”๋ฐ,
์ด๋Š” ์ฃผ๋กœ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋˜ render props ๋ฐฉ์‹์œผ๋กœ์จ
ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ํ›…์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ์— ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ถ€๋ชจ์—๊ฒŒ์„œ props๋ฅผ ๋‚ด๋ ค๋ฐ›์ง€ ์•Š๊ณ  ๊ทธ ๊ณผ์ •์„ ๊ฑด๋„ˆ ๋›ฐ์–ด ๊ฐ’์„ ๊ณต์œ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์•„ํ•˜๋‹ˆ
์ „์—ญ์œผ๋กœ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ๋ง๊ณผ๋„ ๋น„์Šทํ•˜๊ฒŒ ๋“ค๋ฆฝ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ๋ง์ด ๋ฌด์ƒ‰ํ•˜๊ฒŒ๋„ Context๋Š” ์ƒํƒœ ๊ด€๋ฆฌ ํˆด์ด ์•„๋‹™๋‹ˆ๋‹ค.

 

๐Ÿ“Œ Context Api๋Š” ์™œ ์ƒํƒœ ๊ด€๋ฆฌ ํˆด์ด ์•„๋‹Œ๊ฐ€?

๐Ÿ”Ž ๋งŽ์€ Context ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด?

<Context1.Provider value={...}>
    <Context2.Provider value={...}>
        <Context3.Provider value={...}>
            <Context4.Provider value={...}>
                ...
            </Context4.Provider>
        </Context3.Provider>
    </Context2.Provider>
</Context1.Provider>

์šฐ์„  context๋ฅผ ์ผ๋ฐ˜์ ์ธ ์ƒํƒœ ๊ด€๋ฆฌ๋„๊ตฌ๋กœ์จ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด
์œ„์™€ ๊ฐ™์ด ๋ถˆ์–ด๋‚˜๋Š” Provider๋“ค์€ ๊ณ„์†ํ•ด์„œ ์ค‘์ฒฉ๋˜์–ด ์Œ“์—ฌ๊ฐˆ ๊ฒƒ์ด๊ณ ,
ํŠน์ • ์ปดํฌ๋„ŒํŠธ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฐ’์„ ์œ„ํ•ด Provider๋ฅผ ์—ฌ๊ธฐ์ €๊ธฐ ํฉ๋ฟŒ๋ ค ๋†“์œผ๋ฉด ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚ฌ์„ ๋•Œ
๊ทธ๊ฒƒ์„ ๋Œ€์‘ํ•˜๋Š” ๋‚œ์ด๋„๋Š” ๋Š˜์–ด๋‚˜๋Š” context ๋งŒํผ ์–ด๋ ค์›Œ์งˆ ๊ฒ๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๊ธฐ์— Context๋Š” Redux๋‚˜ Recoil ๊ฐ™์€ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์‚ฌ์šฉ์„ ๋ฐฐ์ œํ•˜์ง€ ์•Š๊ณ ,
์‹ค์ œ๋กœ๋„ ์ „์—ญ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์—ฌํƒ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ”Ž ์ƒํƒœ ๊ด€๋ฆฌ ๋„๊ตฌ์˜ ์กฐ๊ฑด

์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋„๊ตฌ๊ฐ€ ๋˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

  • ๊ฐ’ ์ €์žฅ
  • ๊ฐ’ ์ฝ๊ธฐ
  • ๊ฐ’ ๋ณ€๊ฒฝ

Context๋Š” ์ž์ฒด์ ์œผ๋กœ ์–ด๋”˜๊ฐ€์— ๊ฐ’์„ ์ €์žฅํ•˜์ง€๋„ ์•Š๊ณ 
useState์˜ setState์ฒ˜๋Ÿผ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€๋„ ์•Š์Šต๋‹ˆ๋‹ค.

 

๊ทธ์ € Provider์— ๋„ฃ์–ด์ค€ value๋ฅผ ํ•˜์œ„ Children ์•„๋ฌด๊ณณ์—์„œ๋‚˜
useContext๋ฅผ ์‚ฌ์šฉํ•ด ๊บผ๋‚ด์“ฐ๊ธฐ๋งŒ ํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค.

 

์ฆ‰, Context๋Š” ์•„๋ฌด๊ฒƒ๋„ ๊ด€๋ฆฌํ•˜์ง€ ์•Š๊ณ  ์ค‘๊ฐ„๋‹ค๋ฆฌ์˜ ์—ญํ• ๋งŒ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๊ธฐ์— ํŠน์ • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ๊ธฐ๋Šฅ์„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ƒํ™”ํ•œ ํ›„
์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™•์žฅํ•œ ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ Context์— ์ฃผ์ž…ํ•˜๊ณ ,
์ด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” Context๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๊ทธ ๊ธฐ๋Šฅ์„ ์ง์ ‘์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ
์ถ”์ƒํ™”ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ฒŒ ํ•˜์—ฌ ์‰ฝ๊ฒŒ ๊ต์ฒด ๊ฐ€๋Šฅํ•˜๊ฒŒํ•จ์œผ๋กœ์จ
์˜์กด์„ฑ ์ฃผ์ž…์˜ ๊ฐœ๋…์„ ๊ฐ€์กŒ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ Context์™€ ์˜์กด์„ฑ ์ฃผ์ž…

Context๋ฅผ ๊ณ„์ธต ์ค‘๊ฐ„์— ๋ฐฐ์น˜ํ•œ๋‹ค๋ฉด ๊ณ„์ธต๊ฐ„์˜ ๊ฒฐํ•ฉ์„ ๋Š์–ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ๋กœ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

const getAxiosData = useCallback(async () => {
  const data = await axios.get<any>(
    "https://jsonplaceholder.typicode.com/posts"
  );
  return data;
}, []);

๊ธฐ์กด์—๋Š” ์„œ๋ฒ„์™€์˜ ํ†ต์‹ ์„ ํ•  ๋•Œ ํŠน์ • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณค ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๋ชจ๋“  ์ฝ”๋“œ์—๋Š” axios๋ฅผ ์ง์ ‘์ ์œผ๋กœ ํ˜ธ์ถœํ•  ๊ฒƒ์ด๊ณ ,
๋งŒ์•ฝ axios์— ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค๊ฑฐ๋‚˜, ํ˜น์€ ๋‹ค๋ฅธ ๊ธฐ์ˆ ์˜ ๋ฉ”๋ฆฌํŠธ๊ฐ€ ์ปค๋ณด์—ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ต์ฒดํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด
๊ธฐ์กด์— ์ž‘์„ฑํ–ˆ๋˜ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ํŠน์ • ๊ธฐ๋Šฅ ์ž์ฒด๋ฅผ context์— ์ฃผ์ž…ํ•˜์—ฌ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”Ž ํƒ€์ž… ์ •์˜

export interface FetchInterface {
  get<T>(url: string, params?: any | any[]): Promise<T>;
  post<T>(url: string, params?: any | any[]): Promise<T>;
}

๊ธฐ๋ณธ์ ์œผ๋กœ rest api๋กœ ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•  ๋•Œ์—๋Š” ์ฃผ๋กœ get, post, patch, update, delete๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

ํŽธ์˜๋ฅผ ์œ„ํ•ด get๊ณผ post์˜ ๊ธฐ๋Šฅ๋งŒ ์ถ”์ƒํ™”ํ•ด ํƒ€์ž…์œผ๋กœ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”Ž ๊ฐ์ฒด ์ƒ์„ฑ

export class Axios implements FetchInterface {
  async get<T>(url: string, params?: any | any[]): Promise<T> {
    const { data } = await axios.get<T>(url, { params });
    return data;
  }
  async post<T>(url: string, params?: any | any[]): Promise<T> {
    const { data } = await axios.post(url, { params });
    return data;
  }
}
export class Fetch implements FetchInterface {
  async get<T>(url: string, params?: any | any[]): Promise<T> {
    const data = await fetch(url, {
      method: "GET",
      body: JSON.stringify(params)
    }).then((response) => response.json());
    return data;
  }
  async post<T>(url: string, params?: any | any[]): Promise<T> {
    const data = await fetch(url, {
      method: "POST",
      body: JSON.stringify(params)
    }).then((response) => response.json());
    return data;
  }
}

์œ„์—์„œ ์ •์˜ํ•œ ํƒ€์ž…์„ ํด๋ž˜์Šค์—์„œ ํ™•์žฅํ•ด ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ๊ธฐ์ˆ ํ•œ ํ˜•์‹๋Œ€๋กœ ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”Ž Context ์ •์˜

// context ๊ฐ์ฒด ์ƒ์„ฑ
const FetchContext = React.createContext<FetchInterface>(new Fetch());

// Provider์— ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ value๋กœ ๋„ฃ์–ด์ค€๋‹ค.
// ์ถ”ํ›„ fetching ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ณ€๊ฒฝ ์‹œ ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜๋Œ€๋กœ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑ ํ›„,
// value์— ์ธ์Šคํ„ด์Šค๋งŒ ๊ฐˆ์•„ ๋ผ์›Œ์ฃผ๋ฉด ๋œ๋‹ค.
export const FetchContextProvider = ({
  children
}: {
  children: JSX.Element;
}) => {
  return (
    <FetchContext.Provider value={new Axios()}>
      {children}
    </FetchContext.Provider>
  );
};

// useContext๋ฅผ custom hook ํ˜•ํƒœ๋กœ ํ•œ๋ฒˆ ๋” wrapping ํ•ด์„œ
// ์ปดํฌ๋„ŒํŠธ์—์„  ์ด ํ›…๋งŒ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
export const useFetchContext = () => React.useContext(FetchContext);
// Provider์˜ value ๊ต์ฒด๋กœ ๊ธฐ์ˆ  ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝ
return (
  <FetchContext.Provider value={new Fetch()}>
    {children}
  </FetchContext.Provider>
)

์ด์ œ context๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ค€๋น„ํ•ด์ค๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ Provider์˜ value์— ์œ„์—์„œ ์ƒ์„ฑํ•œ ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋งŒ ๋„ฃ์–ด์ค€๋‹ค๋ฉด
๋‹ค๋ฅธ ๋ถ€๋ถ„์˜ ์ˆ˜์ • ์—†์ด ๊ธฐ์ˆ ์˜ ๊ต์ฒด๊ฐ€ ์ด๋ค„์ง„๋‹ค๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

 

๐Ÿ”Ž Context ํ˜ธ์ถœ

export default function App() {
  const [state, setState] = useState<any>();
  const { get } = useFetchContext();

  const getServerData = useCallback(async () => {
    const data = await get<any>("https://jsonplaceholder.typicode.com/posts");
    return data;
  }, [get]);

  useEffect(() => {
    getServerData().then((response) => setState(response));
  }, [getServerData]);

  return (
    <FetchContextProvider>
      <div>{state && JSON.stringify(state)}</div>
    </FetchContextProvider>
  );
}

์‚ฌ์šฉํ•˜๋Š” ๊ณณ์—์„œ๋Š” ํŠน์ • ๊ธฐ์ˆ ์„ ์ง์ ‘ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  context๋กœ ์ •์˜ํ•œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—
๋กœ์ง๊ณผ ํŠน์ • ๊ธฐ์ˆ ์˜ ๋ถ„๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ํŠน์ • ๊ธฐ๋Šฅ์ด๋‚˜ ๊ธฐ์ˆ ์„ ์ •ํ•ด์ง„ ํ˜•์‹๋Œ€๋กœ ์ •์˜ํ•ด ๋†“์œผ๋ฉด
์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” ๋ถˆํ•„์š”ํ•œ ๋ณ€๊ฒฝ์— ์ž์œ ๋กœ์›Œ์งˆ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ
ํ…Œ์ŠคํŠธํ•  ๋•Œ๋Š” ์ƒ์œ„์—์„œ mocking Provider๋งŒ ์ œ๊ณตํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค๋Š” ์žฅ์  ๋˜ํ•œ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

์ „์ฒด ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์—์„œ ํ™•์ธํ•ด์ฃผ์„ธ์š”.

 

 

๐Ÿ“Œ ๋งˆ์น˜๋ฉฐ

์ƒ๊ฐ๋ณด๋‹ค ์ดํ•ดํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š์€ ๋‚ด์šฉ์ด์ง€๋งŒ ๋ชจ๋‹ฌ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด๋‚˜ Data Fetching ๊ฐ™์€ ๊ธฐ์ˆ ๋“ค์„
context๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ถ”ํ›„ ๊ธฐ๋ฐ˜ ๊ธฐ์ˆ ์„ ๋ณ€๊ฒฝํ•  ์ผ์ด ์žˆ์–ด๋„ ์žฌ์‚ฌ์šฉ ํ•˜๊ธฐ ์‰ฌ์›Œ์ง€๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์•„์ง๊นŒ์ง„ ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๊ธฐ์—” ์ดํ•ด๋„๊ฐ€ ๋ถ€์กฑํ•˜๋‹ค๊ณ  ๋Š๋ผ์ง€๋งŒ ์–ธ์  ๊ฐ„ ๊ผญ ์ ์šฉํ•ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


์˜คํ”ˆ ์ฑ„ํŒ