Search

Context

Context

๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

Context ์˜ ํ•„์š”์„ฑ

Context ๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒํƒœ๋ฅผ ๊ณต์œ ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. - ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ - ์ปดํฌ๋„ŒํŠธ 2๊ณ„์ธต ์ด์ƒ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ
์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋‹ค ๋ณด๋ฉด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ ์™ธ์—๋„ ์ž์‹์—์„œ ๋ถ€๋ชจ๋กœ, ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ ์—ฌ๋Ÿฌ ๊ณ„์ธต์„ ๋„˜์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, Global State ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
Global State๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, Global State ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
โ€ข
๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋„ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ, React Context ๋ฅผ ๋„์ž…ํ•˜๋ฉด ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. React Context ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง‘๋‹ˆ๋‹ค.
โ€ข
์œ ์ง€ ๊ด€๋ฆฌ๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค.

Context vs Props

์ผ๋ฐ˜์ ์œผ๋กœ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š”, props ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ์ด๋ ‡๊ฒŒ ๋‹จ์ˆœํžˆ 1๊ณ„์ธต ์ •๋„์˜ ๊ด€๊ณ„์—์„œ๋Š” props ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ฒ ์ง€๋งŒ, 2๊ณ„์ธต, 3๊ณ„์ธต.. .N๊ณ„์ธต ์ด์ƒ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์ด ํ•„์š”ํ•˜๋‹ค๋ฉด, ๊ณ„์†์ ์œผ๋กœ props ๋ฅผ ๋‚ด๋ ค์ฃผ๊ฒŒ ๋˜์–ด ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง€๊ณ  ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐœ๋…์ด ๋ฐ”๋กœ Context ์ž…๋‹ˆ๋‹ค.
์ปจํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•œ ๊ณ„์ธต์”ฉ ๊ณ„์†์ ์œผ๋กœ props ๋ฅผ ๋‚ด๋ฆด ํ•„์š”์—†์ด ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ”๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, props ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋‚ด๋ฆด ํ•„์š”์—†์–ด ์ฝ”๋“œ๋„ ๊ฐ„๊ฒฐํ•ด์ง€๊ณ  ์œ ์ง€๋ณด์ˆ˜์„ฑ๋„ ์˜ฌ๋ผ๊ฐ‘๋‹ˆ๋‹ค.

Context ์‚ฌ์šฉ ๋ชฉ์ 

์ปจํ…์ŠคํŠธ๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ณตํ†ต์ ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ž์ฃผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๋ฐ์ดํ„ฐ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋กœ๊ทธ์ธ ์—ฌ๋ถ€์™€ ๋กœ๊ทธ์ธ ์ •๋ณด ๋“ฑ์ด ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ํŽ˜์ด์ง€ ์ด๋™์„ ์—ฌ๋Ÿฌ ๋ฒˆ ํ•˜๋ฉด์„œ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์กฐ๊ธˆ์”ฉ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” UI ์˜ ๊ฒฝ์šฐ์—๋„, ๋งค๋ฒˆ ํŽ˜์ด์ง€ ์ด๋™ํ•˜๋ฉด์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ธธ ํ•„์š” ์—†์ด ์ปจํ…์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด๋„ ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.

Context ์ฃผ์š” ๊ฐœ๋…

Provider

: Context๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.

Consumer

: Context์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.

Context Value

: Context์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.

React Context ์ ์šฉ๊ณผ์ •

React Context ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‹จ๊ณ„๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
1.
Context ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
2.
Providerย ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
3.
Context ๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์ •์˜๋œ Provider ๋ฅผ ํ†ตํ•ด ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
4.
Consumerย ์ปดํฌ๋„ŒํŠธ ๋˜๋Š” useContext ์‚ฌ์šฉํ•˜์—ฌ Context ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

๋ฌธ๋ฒ•

โ€ข
Context ์ƒ์„ฑ
โ€ข
Provider ์ปดํฌ๋„ŒํŠธ ์ •์˜
โ€ข
Provider ์ ์šฉ
โ€ข
Consumer ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ

Context ์ƒ์„ฑ

โ€ข
๊ธฐ๋ณธ ๋ฌธ๋ฒ•
import { createContext } from 'react'; const ์ปจํ…์ŠคํŠธ์ด๋ฆ„ = createContext(๊ธฐ๋ณธ๊ฐ’);
JavaScript
๋ณต์‚ฌ
import React from 'react'; const ์ปจํ…์ŠคํŠธ์ด๋ฆ„ = React.createContext(๊ธฐ๋ณธ๊ฐ’);
JavaScript
๋ณต์‚ฌ
โ€ข
MyContext.jsx
import { createContext } from 'react'; const MyContext = createContext(); export default MyContext;
JavaScript
๋ณต์‚ฌ

Provider ์ปดํฌ๋„ŒํŠธ ์ •์˜

Provider ์ปดํฌ๋„ŒํŠธ๋Š” React Context๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. Provider ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
๊ธฐ๋ณธ ๋ฌธ๋ฒ•
<MyContext.Provider value={ {๊ฐ’1, ๊ฐ’2, ...} }> {children} </MyContext.Provider>
JavaScript
๋ณต์‚ฌ
โ€ข
MyProvider.jsx
// MyProvider.js import React, { useState } from 'react'; import MyContext from './MyContext'; const MyProvider = ({ children }) => { const [myState, setMyState] = useState('Hello Context~!'); return ( <MyContext.Provider value={{ myState, setMyState }}> {children} </MyContext.Provider> ); }; export default MyProvider;
JavaScript
๋ณต์‚ฌ
value ์†์„ฑ์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ณต์œ ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ ์ง€์ •ํ•œ value ๋ฅผ Consumer ์ปดํฌ๋„ŒํŠธ์—์„œ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Provider ํ•˜์œ„์—์„œ context๋ฅผ ๊ตฌ๋…ํ•˜๋Š” ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋Š” Provider์˜ย valueย prop๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ๋‹ค์‹œ ๋ Œ๋”๋ง ๋ฉ๋‹ˆ๋‹ค.
Provider ์ปดํฌ๋„ŒํŠธ๋Š”ย valueย prop์„ ๋ฐ›์•„์„œ ์ด ๊ฐ’์„ ํ•˜์œ„์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ˆ˜์— ์ œํ•œ์€ ์—†์Šต๋‹ˆ๋‹ค. Provider ํ•˜์œ„์— ๋˜ ๋‹ค๋ฅธ Provider๋ฅผ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ด ๊ฒฝ์šฐ ํ•˜์œ„ Provider์˜ ๊ฐ’์ด ์šฐ์„ ์‹œ๋ฉ๋‹ˆ๋‹ค. - https://ko.legacy.reactjs.org/docs/context.html

Provider ์ ์šฉ

Context ๋กœ ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›์„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
๊ธฐ๋ณธ ๋ฌธ๋ฒ•
// App.js import React from 'react'; import { ์ปดํฌ๋„ŒํŠธ์ด๋ฆ„1 } from './์ปดํฌ๋„ŒํŠธํŒŒ์ผ์ด๋ฆ„1'; import { ์ปดํฌ๋„ŒํŠธ์ด๋ฆ„2 } from './์ปดํฌ๋„ŒํŠธํŒŒ์ผ์ด๋ฆ„2'; import Provider์ด๋ฆ„ from './ProviderํŒŒ์ผ์ด๋ฆ„'; const App = () => { return ( <Provider์ด๋ฆ„> {/* Context ๋ฅผ ์ œ๊ณต๋ฐ›์„ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค. */} <div> <์ปดํฌ๋„ŒํŠธ์ด๋ฆ„1 /> {/* Context ๋ฅผ ์ œ๊ณต๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. */} <์ปดํฌ๋„ŒํŠธ์ด๋ฆ„2 /> {/* Context ๋ฅผ ์ œ๊ณต๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. */} </div> </Provider์ด๋ฆ„> ); }; export default App;
JavaScript
๋ณต์‚ฌ
โ€ข
App.js
// App.js import React from 'react'; import MyClassComponent from './MyClassComponent'; import MyFunctionalComponent from './MyFunctionalComponent'; import MyProvider from '.contexts/MyProvider'; const App = () => { return ( <MyProvider> <div> <MyClassComponent /> <MyFunctionalComponent /> </div> </MyProvider> ); }; export default App;
JavaScript
๋ณต์‚ฌ

Consumer ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ

โ€ข
ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ Consumer ์‚ฌ์šฉ
ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ Consumer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
import ์ปจํ…์ŠคํŠธ์ด๋ฆ„ from './์ปจํ…์ŠคํŠธํŒŒ์ผ์ด๋ฆ„'; class ํด๋ž˜์Šค์ปดํฌ๋„ŒํŠธ extends React.Component { render() { return ( <์ปจํ…์ŠคํŠธ์ด๋ฆ„.Consumer> {({ ๊ฐ’1, ๊ฐ’2( ) }) => ( <div> <p>{๊ฐ’1}</p> <button onClick={() => ๊ฐ’2('๋ณ€๊ฒฝํ•  ๊ฐ’')}>์ปจํ…์ŠคํŠธ ๋ณ€๊ฒฝ</button> </div> )} </์ปจํ…์ŠคํŠธ์ด๋ฆ„.Consumer> ); } } export default ํด๋ž˜์Šค์ปดํฌ๋„ŒํŠธ;
JavaScript
๋ณต์‚ฌ
โ€ข
ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ Consumer ์‚ฌ์šฉ
ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useContext() Hook์œผ๋กœ Consumer๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
import React, { useContext } from 'react'; import ์ปจํ…์ŠคํŠธ์ด๋ฆ„ from './์ปจํ…์ŠคํŠธํŒŒ์ผ์ด๋ฆ„'; // ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ useContext ์‚ฌ์šฉ const ํ•จ์ˆ˜ํ˜•์ปดํฌ๋„ŒํŠธ = () => { const { ๊ฐ’1, ๊ฐ’2 } = useContext(์ปจํ…์ŠคํŠธ์ด๋ฆ„); return ( <div> <p>{๊ฐ’1}</p> <button onClick={() => ๊ฐ’2('๋ณ€๊ฒฝํ•  ๊ฐ’')}>์ปจํ…์ŠคํŠธ ๋ณ€๊ฒฝ</button> </div> ); }; export default ํ•จ์ˆ˜ํ˜•์ปดํฌ๋„ŒํŠธ;
JavaScript
๋ณต์‚ฌ
1.
ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ Consumer ์‚ฌ์šฉ
import React from 'react'; import MyContext from './MyContext'; // ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ Consumer ์‚ฌ์šฉ class MyClassComponent extends React.Component { render() { return ( <MyContext.Consumer> {({ myState, setMyState }) => ( <div> <p>{myState}</p> <button onClick={() => setMyState('Updated!')}>Update State</button> </div> )} </MyContext.Consumer> ); } } export default MyClassComponent;
JavaScript
๋ณต์‚ฌ
2.
ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ Consumer ์‚ฌ์šฉ
import React, { useContext } from 'react'; import MyContext from './MyContext'; // ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ useContext ์‚ฌ์šฉ const MyFunctionalComponent = () => { const { myState, setMyState } = useContext(MyContext); return ( <div> <p>{myState}</p> <button onClick={() => setMyState('Updated!')}>Update State</button> </div> ); }; export default MyFunctionalComponent;
JavaScript
๋ณต์‚ฌ

Context ์ถœ๋ ฅ์ด๋ฆ„ ์„ค์ • - displayName

export const MyContext = React.createContext(); MyContext.displayName = 'MyContextName'
JavaScript
๋ณต์‚ฌ
๋ธŒ๋ผ์šฐ์ €์˜ React ๊ฐœ๋ฐœ๋„๊ตฌ์—์„œ ๋žœ๋”๋ง๋œ ์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ ์ฝ”๋“œ

Context ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๊ทธ์ธ ์—ฌ๋ถ€๋ฅผ ์ฒ˜๋ฆฌ๋Š” ์˜ˆ์‹œ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค.

๋กœ๊ทธ์ธ์ด ๋˜์ง€ ์•Š์•˜์„ ๋•Œ

โ€ข
Context ์˜ ๋กœ๊ทธ์ธ ์—ฌ๋ถ€ ๋ฐ์ดํ„ฐ(isLogin)๊ฐ€ false ์ผ ๋•Œ

๋กœ๊ทธ์ธ ๋˜์—ˆ์„ ๋•Œ ํ™”๋ฉด

โ€ข
Context ์˜ ๋กœ๊ทธ์ธ ์—ฌ๋ถ€ ๋ฐ์ดํ„ฐ(isLogin)๊ฐ€ true ์ผ ๋•Œ

์„ค์น˜

โ€ข
React ์•ฑ ์ƒ์„ฑ
npx create-react-app context-app
Shell
๋ณต์‚ฌ
โ€ข
router ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜
npm install react-router-dom
Shell
๋ณต์‚ฌ

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

์ž‘์—…ํŒŒ์ผ

โ€ข
./contexts/LoginContextProvider.jsx
โ€ข
./contexts/LoginContextConsumer.jsx
โ€ข
./components/Header/Header.jsx
โ€ข
./components/Header/Header.css
โ€ข
./pages/Home.jsx
โ€ข
./pages/About.jsx
โ€ข
./pages/Login.jsx
โ€ข
./pages/Join.jsx
โ€ข
./pages/User.jsx
โ€ข
App.js
โ€ข
index.css
โ€ข
App.css

./contexts/LoginContextProvider.jsx

import React, { useEffect, useState } from 'react'; export const LoginContext = React.createContext(); LoginContext.displayName = 'LoginContextName' const LoginContextProvider = ({ children }) => { const [isLogin, setIsLogin] = useState(false); const logout = () => { setIsLogin(false) } useEffect(() => { setTimeout(() => { setIsLogin(true); }, 3000); }, []); return ( <LoginContext.Provider value={ {isLogin, logout} }> {children} </LoginContext.Provider> ); }; export default LoginContextProvider;
JavaScript
๋ณต์‚ฌ

./contexts/LoginContextConsumer.jsx

import React, { useContext } from 'react'; import { LoginContext } from './LoginContextProvider'; const LoginContextConsumer = ({ children }) => { const { isLogin } = useContext(LoginContext); return ( <div> <h3>๋กœ๊ทธ์ธ ์—ฌ๋ถ€: {isLogin ? '๋กœ๊ทธ์ธ' : '๋กœ๊ทธ์•„์›ƒ'}</h3> {children} </div> ); }; export default LoginContextConsumer;
JavaScript
๋ณต์‚ฌ

./components/Header/Header.jsx

import React, { useContext } from 'react'; import { Link } from 'react-router-dom' import './Header.css' import { LoginContext } from '../../contexts/LoginContextProvider'; const Header = () => { // โœ… isLogin : ๋กœ๊ทธ์ธ ์—ฌ๋ถ€ - Y(true), N(false) // โœ… logout() : ๋กœ๊ทธ์•„์›ƒ ํ•จ์ˆ˜ - setIsLogin(false) const { isLogin, logout } = useContext(LoginContext); return ( <header> <div className="logo"> <Link to="/"> <img src="https://i.imgur.com/fzADqJo.png" alt="logo" className='logo' /> </Link> </div> <div className="util"> <ul> {/* ๋กœ๊ทธ์ธ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์กฐ๊ฑด๋ถ€ ๋žœ๋”๋ง */} { !isLogin ? <> <li><Link to="/login">๋กœ๊ทธ์ธ</Link></li> <li><Link to="/join">ํšŒ์›๊ฐ€์ž…</Link></li> <li><Link to="/about">์†Œ๊ฐœ</Link></li> </> : <> <li><Link to="/user">๋งˆ์ดํŽ˜์ด์ง€</Link></li> <li><button className='link' onClick={ () => logout() }>๋กœ๊ทธ์•„์›ƒ</button></li> </> } </ul> </div> </header> ) } export default Header
JavaScript
๋ณต์‚ฌ

./components/Header/Header.css

header { display: flex; justify-content: space-between; align-items: center; padding: 20px; border-bottom: 1px solid #000; } .logo { width: 80px; } .util ul { display: flex; justify-content: space-between; align-items: center; column-gap: 12px; } .util ul li { }
CSS
๋ณต์‚ฌ

./pages/Home.jsx

import React from 'react'; import LoginContextConsumer from '../contexts/LoginContextConsumer'; import Header from '../components/Header/Header'; const Home = () => { return ( <> <Header /> <div className='container'> <h1>Home</h1> <hr/> <h2>๋ฉ”์ธ ํŽ˜์ด์ง€</h2> <LoginContextConsumer /> </div> </> ) } export default Home
JavaScript
๋ณต์‚ฌ

./pages/About.jsx

import React from 'react'; import LoginContextConsumer from '../contexts/LoginContextConsumer'; import Header from '../components/Header/Header'; const About = () => { return ( <> <Header /> <div className='container'> <h1>About</h1> <hr/> <h2>์†Œ๊ฐœ ํŽ˜์ด์ง€</h2> <LoginContextConsumer /> </div> </> ) } export default About
JavaScript
๋ณต์‚ฌ

./pages/Login.jsx

import React from 'react' import Header from '../components/Header/Header' import LoginContextConsumer from '../contexts/LoginContextConsumer' const Login = () => { return ( <> <Header /> <div className='container'> <h1>Login</h1> <hr/> <h2>๋กœ๊ทธ์ธ ํŽ˜์ด์ง€</h2> <LoginContextConsumer /> </div> </> ) } export default Login
JavaScript
๋ณต์‚ฌ

./pages/Join.jsx

import React from 'react' import Header from '../components/Header/Header' import LoginContextConsumer from '../contexts/LoginContextConsumer' const Join = () => { return ( <> <Header /> <div className='container'> <h1>Join</h1> <hr/> <h2>ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€</h2> <LoginContextConsumer /> </div> </> ) } export default Join
JavaScript
๋ณต์‚ฌ

./pages/User.jsx

import React from 'react'; import LoginContextConsumer from '../contexts/LoginContextConsumer'; import Header from '../components/Header/Header'; const Home = () => { return ( <> <Header /> <div className='container'> <h1>User</h1> <hr/> <h2>๋งˆ์ด ํŽ˜์ด์ง€</h2> <LoginContextConsumer /> </div> </> ) } export default Home
JavaScript
๋ณต์‚ฌ

App.js

import React from 'react'; import { BrowserRouter, Route, Routes } from 'react-router-dom'; import './App.css'; import LoginContextProvider from './contexts/LoginContextProvider'; import About from './pages/About'; import Home from './pages/Home'; import User from './pages/User'; import Login from './pages/Login'; import Join from './pages/Join'; const App = () => { return ( <BrowserRouter> <LoginContextProvider> <Routes> <Route path="/" element={<Home />} /> <Route path="/login" element={<Login />} /> <Route path="/join" element={<Join />} /> <Route path="/user" element={<User />} /> <Route path="/about" element={<About />} /> </Routes> </LoginContextProvider> </BrowserRouter> ); }; export default App;
JavaScript
๋ณต์‚ฌ

index.css

@import url('https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap'); * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Noto Sans', sans-serif; font-size: var(--font-size-main); } ul { list-style-type: none; } a { text-decoration: none; color: black; } button { cursor: pointer; } .link { background-color: transparent; color: black; border: none; font-size: var(--font-size-main); } :root { --font-size-main : 20px; }
CSS
๋ณต์‚ฌ

App.css

.container { width: 1024px; margin: 50px auto; } hr { margin-bottom: 50px; }
CSS
๋ณต์‚ฌ