useContext
useContext
๋ ์ปดํฌ๋ํธ์์ Context๋ฅผ ์ฝ๊ณ ๊ตฌ๋
ํ ์ ์๋ React Hook์
๋๋ค.
const value = useContext(SomeContext)
๋ ํผ๋ฐ์ค
useContext(SomeContext)
useContext
๋ฅผ ์ปดํฌ๋ํธ์ ์ต์์ ์์ค์์ ํธ์ถํ์ฌ Context๋ฅผ ์ฝ๊ณ ๊ตฌ๋
ํฉ๋๋ค.
import { useContext } from 'react';
function MyComponent() {
const theme = useContext(ThemeContext);
// ...
์๋ ์์๋ฅผ ์ฐธ๊ณ ํ์ธ์.
๋งค๊ฐ๋ณ์
SomeContext
:createContext
๋ก ์์ฑํ Context์ ๋๋ค. Context ์์ฒด๋ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์ง ์์ผ๋ฉฐ, ์ปดํฌ๋ํธ์์ ์ ๊ณตํ๊ฑฐ๋ ์ฝ์ ์ ์๋ ์ ๋ณด์ ์ข ๋ฅ๋ฅผ ๋ํ๋ ๋๋ค.
๋ฐํ๊ฐ
useContext
๋ ํธ์ถํ๋ ์ปดํฌ๋ํธ์ ๋ํ Context ๊ฐ์ ๋ฐํํฉ๋๋ค. ์ด ๊ฐ์ ํธ๋ฆฌ์์ ํธ์ถํ๋ ์ปดํฌ๋ํธ ์์์ ๊ฐ์ฅ ๊ฐ๊น์ด SomeContext
์ ์ ๋ฌ๋ ๊ฐ์ผ๋ก ๊ฒฐ์ ๋ฉ๋๋ค. Provider๊ฐ ์์ผ๋ฉด ๋ฐํ๋ ๊ฐ์ ํด๋น Context์ ๋ํด createContext
์ ์ ๋ฌํ defaultValue
๊ฐ ๋ฉ๋๋ค. ๋ฐํ๋ ๊ฐ์ ํญ์ ์ต์ ์ํ์
๋๋ค. Context๊ฐ ๋ณ๊ฒฝ๋๋ฉด React๋ ์๋์ผ๋ก ํด๋น Context๋ฅผ ์ฝ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ค์ ๋ ๋๋งํฉ๋๋ค.
์ฃผ์ ์ฌํญ
- ์ปดํฌ๋ํธ ๋ด์
useContext()
ํธ์ถ์ ๋์ผํ ์ปดํฌ๋ํธ์์ ๋ฐํ๋ Provider์ ์ํฅ์ ๋ฐ์ง ์์ต๋๋ค. ํด๋นํ๋<Context>
๋useContext()
ํธ์ถ์ ํ๋ ์ปดํฌ๋ํธ ์์์ ๋ฐฐ์น๋์ด์ผ ํฉ๋๋ค. - React๋ ๋ค๋ฅธ
value
์ ๋ฐ๋ Provider๋ก๋ถํฐ ์์ํด์ ํน์ Context๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ์์๋ค์ ์๋์ผ๋ก ๋ฆฌ๋ ๋๋งํฉ๋๋ค. ์ด์ ๊ฐ๊ณผ ๋ค์ ๊ฐ์Object.is
๋ฅผ ํตํด ๋น๊ตํฉ๋๋ค.memo
๋ก ๋ฆฌ๋ ๋๋ง์ ๊ฑด๋๋ฐ์ด๋ ์์๋ค์ด ์๋ก์ด Context ๊ฐ์ ๋ฐ๋ ๊ฒ์ ๋ง์ง๋ ๋ชปํฉ๋๋ค. - ๋น๋ ์์คํ
์ด ๊ฒฐ๊ณผ๋ฌผ์ ์ค๋ณต ๋ชจ๋์ ์์ฑํ๋ ๊ฒฝ์ฐ(์ฌ๋ณผ๋ฆญ ๋งํฌ์์ ๋ฐ์ํ ์ ์์) Context๊ฐ ์์๋ ์ ์์ต๋๋ค. Context๋ฅผ ํตํด ๋ฌด์ธ๊ฐ๋ฅผ ์ ๋ฌํ๋ ๊ฒ์
===
๋น๊ต์ ์ํด ๊ฒฐ์ ๋๋ ๊ฒ์ฒ๋ผ Context๋ฅผ ์ ๊ณตํ๋ ๋ฐ ์ฌ์ฉํ๋SomeContext
์ Context๋ฅผ ์ฝ๋ ๋ฐ ์ฌ์ฉํ๋SomeContext
๊ฐ ์ ํํ๊ฒ ๋์ผํ ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ์๋ง ์๋ํฉ๋๋ค.
์ฌ์ฉ๋ฒ
ํธ๋ฆฌ์ ๊น์ ๊ณณ์ ๋ฐ์ดํฐ ์ ๋ฌํ๊ธฐ
์ปดํฌ๋ํธ์ ์ต์์ ์์ค์์ useContext
๋ฅผ ํธ์ถํ์ฌ Context๋ฅผ ์ฝ๊ณ ๊ตฌ๋
ํฉ๋๋ค.
import { useContext } from 'react';
function Button() {
const theme = useContext(ThemeContext);
// ...
useContext
๋ ์ ๋ฌํ Context์ ๋ํ Context Value๋ฅผ ๋ฐํํฉ๋๋ค. Context ๊ฐ์ ๊ฒฐ์ ํ๊ธฐ ์ํด React๋ ์ปดํฌ๋ํธ ํธ๋ฆฌ๋ฅผ ํ์ํ๊ณ ํน์ Context์ ๋ํด ์์์์ ๊ฐ์ฅ ๊ฐ๊น์ด Context Provider๋ฅผ ์ฐพ์ต๋๋ค.
Context๋ฅผ Button
์ ์ ๋ฌํ๋ ค๋ฉด ํด๋น ๋ฒํผ ๋๋ ์์ ์ปดํฌ๋ํธ ์ค ํ๋๋ฅผ ํด๋น Context Provider๋ก ๊ฐ์๋๋ค.
function MyPage() {
return (
<ThemeContext value="dark">
<Form />
</ThemeContext>
);
}
function Form() {
// ... ๋ด๋ถ์์ ๋ฒํผ์ ๋ ๋๋งํฉ๋๋ค. ...
}
Provider์ Button
์ฌ์ด์ ์ผ๋ง๋ ๋ง์ ์ปดํฌ๋ํธ ๋ ์ด์ด๊ฐ ์๋์ง๋ ์ค์ํ์ง ์์ต๋๋ค. Form
๋ด๋ถ์ Button
์ด ์ด๋์์๋ useContext(ThemeContext)
๋ฅผ ํธ์ถํ๋ฉด,"dark"
๋ฅผ ๊ฐ์ผ๋ก ๋ฐ์ต๋๋ค.
import { createContext, useContext } from 'react'; const ThemeContext = createContext(null); export default function MyApp() { return ( <ThemeContext value="dark"> <Form /> </ThemeContext> ) } function Form() { return ( <Panel title="Welcome"> <Button>Sign up</Button> <Button>Log in</Button> </Panel> ); } function Panel({ title, children }) { const theme = useContext(ThemeContext); const className = 'panel-' + theme; return ( <section className={className}> <h1>{title}</h1> {children} </section> ) } function Button({ children }) { const theme = useContext(ThemeContext); const className = 'button-' + theme; return ( <button className={className}> {children} </button> ); }
Context๋ฅผ ํตํด ์ ๋ฌ๋ ๋ฐ์ดํฐ ์ ๋ฐ์ดํธํ๊ธฐ
๋๋๋ก Context๊ฐ ์๊ฐ์ด ์ง๋จ์ ๋ฐ๋ผ ๋ณ๊ฒฝ๋๊ธฐ๋ฅผ ์ํ ๊ฒ์ ๋๋ค. Context๋ฅผ ์ ๋ฐ์ดํธ ํ๋ ค๋ฉด State์ ๊ฒฐํฉํ์ธ์. ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ State๋ณ์๋ฅผ ์ ์ธํ๊ณ ํ์ฌ State๋ฅผ Context Value๋ก Provider์ ์ ๋ฌํฉ๋๋ค.
function MyPage() {
const [theme, setTheme] = useState('dark');
return (
<ThemeContext value={theme}>
<Form />
<Button onClick={() => {
setTheme('light');
}}>
Switch to light theme
</Button>
</ThemeContext>
);
}
์ด์ Provider ๋ด๋ถ์ ๋ชจ๋ Button
์ ํ์ฌ theme
๊ฐ์ ๋ฐ๊ฒ ๋ฉ๋๋ค. Provider์ ์ ๋ฌ๋ theme
๊ฐ์ ์
๋ฐ์ดํธ ํ๊ธฐ ์ํด setTheme
์ ํธ์ถํ๋ฉด, ๋ชจ๋ Button
์ปดํฌ๋ํธ๊ฐ ์๋ก์ด 'light'
๊ฐ์ผ๋ก ๋ค์ ๋ ๋๋ง๋ฉ๋๋ค.
์์ 1 of 5: Context๋ฅผ ํตํด ๊ฐ ์
๋ฐ์ดํธ
์ด ์์์์ MyApp
์ปดํฌ๋ํธ๋ State ๋ณ์๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ์ด State ๋ณ์๋ ThemeContext
Provider๋ก ์ ๋ฌ๋ฉ๋๋ค. โUse dark modeโ ์ฒดํฌ๋ฐ์ค๋ฅผ ์ฒดํฌํ๋ฉด State๊ฐ ์
๋ฐ์ดํธ ๋ฉ๋๋ค. ์ ๊ณต๋ ๊ฐ์ ๋ณ๊ฒฝํ๋ฉด ํด๋น Context๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ์ปดํฌ๋ํธ๊ฐ ๋ค์ ๋ ๋๋ง๋ฉ๋๋ค.
import { createContext, useContext, useState } from 'react'; const ThemeContext = createContext(null); export default function MyApp() { const [theme, setTheme] = useState('light'); return ( <ThemeContext value={theme}> <Form /> <label> <input type="checkbox" checked={theme === 'dark'} onChange={(e) => { setTheme(e.target.checked ? 'dark' : 'light') }} /> Use dark mode </label> </ThemeContext> ) } function Form({ children }) { return ( <Panel title="Welcome"> <Button>Sign up</Button> <Button>Log in</Button> </Panel> ); } function Panel({ title, children }) { const theme = useContext(ThemeContext); const className = 'panel-' + theme; return ( <section className={className}> <h1>{title}</h1> {children} </section> ) } function Button({ children }) { const theme = useContext(ThemeContext); const className = 'button-' + theme; return ( <button className={className}> {children} </button> ); }
value="dark"
๋ "dark"
๋ฌธ์์ด์ ์ ๋ฌํ์ง๋ง, value={theme}
๋ JSX ์ค๊ดํธ๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฐ์คํฌ๋ฆฝํธ theme
๋ณ์ ๊ฐ์ ์ ๋ฌํฉ๋๋ค. ์ค๊ดํธ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฌธ์์ด์ด ์๋ Context ๊ฐ๋ ์ ๋ฌํ ์ ์์ต๋๋ค.
Fallback ๊ธฐ๋ณธ๊ฐ ์ง์
React๊ฐ ๋ถ๋ชจ ํธ๋ฆฌ์์ ํน์ Context Provider๋ฅผ ์ฐพ์ ์ ์๋ ๊ฒฝ์ฐ, useContext()
๊ฐ ๋ฐํํ๋ Context ๊ฐ์ ํด๋น Context๋ฅผ ์์ฑํ ๋ ์ง์ ํ ๊ธฐ๋ณธ๊ฐ๊ณผ ๋์ผํฉ๋๋ค.
const ThemeContext = createContext(null);
๊ธฐ๋ณธ๊ฐ์ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค. Context๋ฅผ ์ ๋ฐ์ดํธํ๋ ค๋ฉด ์์์ ์ค๋ช ํ ๋๋ก State์ ํจ๊ป ์ฌ์ฉํ์ธ์.
์๋ฅผ ๋ค์ด null
๋์ ์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ฌ์ฉํ ์ ์๋ ๋ ์๋ฏธ ์๋ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
const ThemeContext = createContext('light');
์ด๋ ๊ฒ ํ๋ฉด ์ค์๋ก ํด๋น Provider ์์ด ์ผ๋ถ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํด๋ ๊นจ์ง์ง ์์ต๋๋ค. ๋ํ ํ ์คํธ ํ๊ฒฝ์์ ๋ง์ Provider๋ฅผ ์ค์ ํ์ง ์๊ณ ๋ ์ปดํฌ๋ํธ๊ฐ ํ ์คํธ ํ๊ฒฝ์์ ์ ์๋ํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
์๋ ์์์์ โToggle themeโ ๋ฒํผ์ ํ
๋ง Context Provider์ ์ธ๋ถ์ ์๊ณ ๊ธฐ๋ณธ ์ปจํ
์คํธ ํ
๋ง ๊ฐ์ด 'light'
์ด๊ธฐ ๋๋ฌธ์ ํญ์ ๋ฐ๊ฒ ํ์๋์ด ์์ต๋๋ค. ๊ธฐ๋ณธ ํ
๋ง๋ฅผ 'dark'
๋ก ๋ณ๊ฒฝํด ๋ณด์ธ์.
import { createContext, useContext, useState } from 'react'; const ThemeContext = createContext('light'); export default function MyApp() { const [theme, setTheme] = useState('light'); return ( <> <ThemeContext value={theme}> <Form /> </ThemeContext> <Button onClick={() => { setTheme(theme === 'dark' ? 'light' : 'dark'); }}> Toggle theme </Button> </> ) } function Form({ children }) { return ( <Panel title="Welcome"> <Button>Sign up</Button> <Button>Log in</Button> </Panel> ); } function Panel({ title, children }) { const theme = useContext(ThemeContext); const className = 'panel-' + theme; return ( <section className={className}> <h1>{title}</h1> {children} </section> ) } function Button({ children, onClick }) { const theme = useContext(ThemeContext); const className = 'button-' + theme; return ( <button className={className} onClick={onClick}> {children} </button> ); }
ํธ๋ฆฌ์ ์ผ๋ถ Context ์ค๋ฒ๋ผ์ด๋ฉ ํ๊ธฐ
ํธ๋ฆฌ์ ์ผ๋ถ๋ถ์ ๋ค๋ฅธ ๊ฐ์ Provider๋ก ๊ฐ์ธ์ ํด๋น ๋ถ๋ถ์ ๋ํ Context๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํ ์ ์์ต๋๋ค.
<ThemeContext value="dark">
...
<ThemeContext value="light">
<Footer />
</ThemeContext>
...
</ThemeContext>
ํ์ํ ๋งํผ Provider๋ฅผ ์ค์ฒฉํ๊ณ ์ค๋ฒ๋ผ์ด๋ฉ ํ ์ ์์ต๋๋ค.
์์ 1 of 2: ํ
๋ง ์ค๋ฒ๋ผ์ด๋
์ฌ๊ธฐ์ Footer
๋ด๋ถ์ ๋ฒํผ์ ์ธ๋ถ์ ๋ฒํผ("dark"
)๊ณผ ๋ค๋ฅธ Context ๊ฐ("light"
)์ ๋ฐ์ต๋๋ค.
import { createContext, useContext } from 'react'; const ThemeContext = createContext(null); export default function MyApp() { return ( <ThemeContext value="dark"> <Form /> </ThemeContext> ) } function Form() { return ( <Panel title="Welcome"> <Button>Sign up</Button> <Button>Log in</Button> <ThemeContext value="light"> <Footer /> </ThemeContext> </Panel> ); } function Footer() { return ( <footer> <Button>Settings</Button> </footer> ); } function Panel({ title, children }) { const theme = useContext(ThemeContext); const className = 'panel-' + theme; return ( <section className={className}> {title && <h1>{title}</h1>} {children} </section> ) } function Button({ children }) { const theme = useContext(ThemeContext); const className = 'button-' + theme; return ( <button className={className}> {children} </button> ); }
๊ฐ์ฒด์ ํจ์๋ฅผ ์ ๋ฌํ ๋ ๋ฆฌ๋ ๋๋ง ์ต์ ํํ๊ธฐ
Context๋ฅผ ํตํด ๊ฐ์ฒด์ ํจ์๋ฅผ ํฌํจํ ๋ชจ๋ ๊ฐ์ ์ ๋ฌํ ์ ์์ต๋๋ค.
function MyApp() {
const [currentUser, setCurrentUser] = useState(null);
function login(response) {
storeCredentials(response.credentials);
setCurrentUser(response.user);
}
return (
<AuthContext value={{ currentUser, login }}>
<Page />
</AuthContext>
);
}
์ฌ๊ธฐ์ Context Value๋ ๋ ๊ฐ์ ํ๋กํผํฐ๋ฅผ ๊ฐ์ง ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด์ด๋ฉฐ, ๊ทธ ์ค ํ๋๋ ํจ์์
๋๋ค. MyApp
์ด ๋ค์ ๋ ๋๋งํ ๋๋ง๋ค(์๋ฅผ ๋ค์ด ๊ฒฝ๋ก ์
๋ฐ์ดํธ ์) ๋ค๋ฅธ ํจ์๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ค๋ฅธ ๊ฐ์ฒด๊ฐ ๋ ๊ฒ์ด๋ฏ๋ก React๋ useContext(AuthContext)
๋ฅผ ํธ์ถํ๋ ํธ๋ฆฌ ๊น์ํ ๊ณณ์ ์๋ ๋ชจ๋ ์ปดํฌ๋ํธ๋ ๋ค์ ๋ ๋๋งํด์ผ ํฉ๋๋ค.
์์ ์ฑ์์๋ ๋ฌธ์ ๊ฐ ๋์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ currentUser
์ ๊ฐ์ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋์ง ์์๋ค๋ฉด ๋ค์ ๋ ๋๋งํ ํ์๊ฐ ์์ต๋๋ค. React๊ฐ ์ด ์ฌ์ค์ ํ์ฉํ ์ ์๋๋ก login
ํจ์๋ฅผ useCallback
์ผ๋ก ๊ฐ์ธ๊ณ ๊ฐ์ฒด ์์ฑ์ useMemo
๋ก ๊ฐ์ธ๋ฉด ๋ฉ๋๋ค. ์ด๊ฒ์ด ์ฑ๋ฅ ์ต์ ํ์
๋๋ค.
import { useCallback, useMemo } from 'react';
function MyApp() {
const [currentUser, setCurrentUser] = useState(null);
const login = useCallback((response) => {
storeCredentials(response.credentials);
setCurrentUser(response.user);
}, []);
const contextValue = useMemo(() => ({
currentUser,
login
}), [currentUser, login]);
return (
<AuthContext value={contextValue}>
<Page />
</AuthContext>
);
}
์ด ๋ณ๊ฒฝ์ผ๋ก ์ธํด MyApp
์ด ๋ค์ ๋ ๋๋งํด์ผ ํ๋ ๊ฒฝ์ฐ์๋ currentUser
๊ฐ ๋ณ๊ฒฝ๋์ง ์๋ ํ useContext(AuthContext)
๋ฅผ ํธ์ถํ๋ ์ปดํฌ๋ํธ๋ ๋ค์ ๋ ๋๋งํ ํ์๊ฐ ์์ต๋๋ค.
useMemo
์ useCallback
์ ๋ํด ์์ธํ ์์๋ณด์ธ์.
๋ฌธ์ ํด๊ฒฐ
์ปดํฌ๋ํธ๊ฐ Provider์์ ๊ฐ์ ์ธ์ํ์ง ๋ชปํ๊ณ ์์ต๋๋ค.
์ด๋ฐ ์ผ์ด ๋ฐ์ํ๋ ๋ช ๊ฐ์ง ์ด์ ๊ฐ ์์ต๋๋ค.
useContext()
๋ฅผ ํธ์ถํ๋ ์ปดํฌ๋ํธ์ ๋์ผํ ์ปดํฌ๋ํธ(๋๋ ๊ทธ ์๋)์์<SomeContext>
๋ฅผ ๋ ๋๋งํ๋ ๊ฒฝ์ฐ,<SomeContext>
๋ฅผuseContext()
๋ฅผ ํธ์ถํ๋ ์ปดํฌ๋ํธ์ ์์ ๋ฐ๊นฅ์ผ๋ก ์ด๋ํ์ธ์.- ์ปดํฌ๋ํธ๋ฅผ
<SomeContext>
๋ก ๊ฐ์ธ๋ ๊ฒ์ ์์๊ฑฐ๋ ์๊ฐํ๋ ๊ฒ๊ณผ ๋ค๋ฅธ ํธ๋ฆฌ์ ๋ค๋ฅธ ๋ถ๋ถ์ ๋ฐฐ์นํ์ ์ ์์ต๋๋ค. React ๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ ๊ณ์ธต ๊ตฌ์กฐ๊ฐ ์ฌ๋ฐ๋ฅธ์ง ํ์ธํ์ธ์. - ์ฌ์ฉ ์ค์ธ ๋๊ตฌ์์ ๋ฐ์ํ๋ ๋น๋ ๋ฌธ์ ๋ก ์ธํด, ์ ๊ณตํ๋ ์ปดํฌ๋ํธ์์์
someContext
์ ๊ฐ์ ์ฝ๋ ์ปดํฌ๋ํธ์์์someContext
๊ฐ ์๋ก ๋ค๋ฅธ ๊ฐ์ฒด๋ก ์ฒ๋ฆฌ๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ์ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ด๋ฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํ์ธํ๋ ค๋ฉดwindow.SomeContext1
๊ณผwindow.SomeContext2
๋ฅผ ์ ์ญ์ ํ ๋นํ๊ณ ์ฝ์์์window.SomeContext1 === window.SomeContext2
์ธ์ง ํ์ธํ๋ฉด ๋ฉ๋๋ค. ๋์ผํ์ง ์์ ๊ฒฝ์ฐ ๋น๋ ๋๊ตฌ ์์ค์์ ํด๋น ๋ฌธ์ ๋ฅผ ์์ ํ์ธ์.
๊ธฐ๋ณธ๊ฐ์ด ๋ค๋ฅธ๋ฐ๋ Context๊ฐ undefined
๋ฅผ ๋ฐํํฉ๋๋ค.
ํธ๋ฆฌ์ value
๊ฐ ์๋ Provider๊ฐ ์์ ์ ์์ต๋๋ค.
// ๐ฉ Doesn't work: no value prop
<ThemeContext>
<Button />
</ThemeContext>
value
๋ฅผ ์ง์ ํ๋ ๊ฒ์ ์์ด๋ฒ๋ฆฐ ๊ฒฝ์ฐ, value={undefined}
๋ฅผ ์ ๋ฌํ๋ ๊ฒ๊ณผ ๊ฐ์ต๋๋ค.
์ค์๋ก ๋ค๋ฅธ Prop์ ์ด๋ฆ์ ์ค์๋ก ์ฌ์ฉํ์ ์๋ ์์ต๋๋ค.
// ๐ฉ Doesn't work: prop should be called "value"
<ThemeContext theme={theme}>
<Button />
</ThemeContext>
๋ ๊ฐ์ง ๊ฒฝ์ฐ ๋ชจ๋ ์ฝ์์ React์์ ๊ฒฝ๊ณ ๊ฐ ํ์๋ ๊ฒ์
๋๋ค. ์ด๋ฅผ ์์ ํ๋ ค๋ฉด Prop value
๋ฅผ ํธ์ถํ์ธ์.
// โ
Passing the value prop
<ThemeContext value={theme}>
<Button />
</ThemeContext>
createContext(defaultValue)
ํธ์ถ์ ๊ธฐ๋ณธ๊ฐ์ ์์ ์ผ์นํ๋ Provider๊ฐ ์ ํ ์๋ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉ๋๋ค๋ ์ ์ ์ ์ํ์ธ์. ๋ถ๋ชจ ํธ๋ฆฌ ์ด๋๊ฐ์ <SomeContext value={undefined}>
์ปดํฌ๋ํธ๊ฐ ์๋ ๊ฒฝ์ฐ, useContext(SomeContext)
๋ฅผ ํธ์ถํ๋ ์ปดํฌ๋ํธ๋ undefined
๋ฅผ Context ๊ฐ์ผ๋ก ๋ฐ์ต๋๋ค.