-
[React] useContext ์ฌ์ฉ๋ฒ _(๋ฆฌ์กํธ Hooks)JavaScript/React 2023. 6. 21. 13:29728x90๋ฐ์ํ
๐ง React Hooks์ useContext๋ฅผ ์์๋ณด์
props๋ฅผ ์ฌ์ฉํ ๋ ๊ฐ๋ ๋นํจ์จ์ ์ด๋ผ๊ณ ๋๋ ๋๊ฐ ์์ฃ . ๊ทธ๋ด ๋ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ด ๋ฐ๋ก context์ ๋๋ค. ๋ณธ ํฌ์คํ ์ react ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ์์ฑ๋์์ต๋๋ค. ์ค๋์ useContext Hook์ ๋ํด ์์๋ณด๊ณ ์ฌ์ฉํด ๋ณผ๊ฒ์ฉ
๐ useContext๋ ๋ฌด์์ผ๊น?
useContext๋ ๋ฆฌ์กํธ hook์ ํ๋๋ก, ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ฑํ context๋ฅผ ์ฝ๊ณ (read), ๊ตฌ๋ (subscribe)ํ ์ ์๊ฒ ํ๋ ๋๊ตฌ์ ๋๋ค.
๐คท๐ปโโ๏ธ context? ๊ทธ๊ฒ ๋ญ๋ฐ์
context๋ ๋ง์ด ์๋ฟ์ง ์์ ์ ์์ต๋๋ค. context๋ฅผ ์ง์ญํ๋ฉด ๋ฌธ๋งฅ, ๋ฐฐ๊ฒฝ, ์ํฉ์ด๋ผ๋ ๋ป์ธ๋ฐ, props์ ๋์์ผ๋ก ๋์จ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ค๊ฐ ์ปดํฌ๋ํธ๋ฅผ ๊ฑฐ์น์ง ์์๋ ๋๋ props๋ผ๊ณ ์๊ฐํ์๋ฉด ํธํฉ๋๋ค.
์๋ฅผ ๋ค์ด prop์ ์ฌ์ฉํ๋ค๋ฉด ์๋์ ๊ฐ์ด prop drilling์ด ๋ฐ์ํ๊ธฐ ๋ง๋ จ์ธ๋ฐ,์ด๋ฌํ ํ์์ ๋ฐฉ์งํ ์ ์๋ ๊ฒ์ด context์ธ ๊ฒ์ ๋๋ค.
context๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ ๊ทธ๋ฆผ์ฒ๋ผ ๋ถ๋ชจ์์ ์ํ๋ ์ปดํฌ๋ํธ์ ๋ฐ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
๐ context ์ฌ์ฉ๋ฒ
๐ context ์์ฑํ๊ธฐ
const MyContext = createContext(null);
context๋ฅผ ์์ฑํ ๋๋ createContext๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
๐ context๋ฅผ ์ฌ์ฉํ ๋ฒ์ ์ค์ ํ๊ธฐ
<MyContext.Provider value={some}> </MyContext.Provider>
context๋ฅผ ์ ๋ฌํ๊ณ ์ถ์ ์ค์ฝํ๋ฅผ ์ง์ ํฉ๋๋ค. ์์๋ ๋ฒจ์์ < ์์ฑํ ์ปจํ ์คํธ๋ช .Provider> ์ปดํฌ๋ํธ๋ก ๊ฐ์ธ์ฃผ๊ณ value๋ก context ๊ฐ some์ ์ ๋ฌํฉ๋๋ค. (์ฌ๊ธฐ์ some์ ์์๋ก ์ง์ ํ ์ด๋ฆ์ ๋๋ค.)
๐ context ์ฌ์ฉํ๊ธฐ
const some = useContext(MyContext);
์์ ์ปดํฌ๋ํธ์์ context๋ฅผ ์ฌ์ฉํ ๋๋ useContext๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๊ดํธ ์์ ์ธ์๋ก ์์ฑํ context๋ฅผ ๋ฃ์ด์ฃผ๊ณ ๋ณ์์ ๋ฐ์์์ ์ฌ์ฉํฉ๋๋ค.
๐ ์ด์ ์ง์ ์ฌ์ฉํด๋ณด์
context๋ฅผ ์ด์ฉํด์ ์๋์ ๊ฐ์ form์ ํ ๋ง๋ฅผ ๊ด๋ฆฌํด๋ณผ๊ฒ์. ํ ๋ง๊ฐ dark์ธ์ง light์ธ์ง context๋ก ๊ด๋ฆฌํ๊ณ ์์ ์ปดํฌ๋ํธ์์๋ ์ ์ฉํด ๋ณด๊ฒ ์ต๋๋ค.
๐ createContext ํจ์๋ฅผ ์ฌ์ฉํด์ ์ context ์์ฑํ๊ธฐ
const ThemeContext = createContext('light');
ThemeContext๋ผ๊ณ ํ๋ ๋ฌธ์์ด ํ ๋ง ์ค์ ์ ๋ด์ context๋ฅผ ์์ฑํฉ๋๋ค.
๋ํดํธ๊ฐ์ light๋ก ์ค์ ํฉ๋๋ค.
๐ ์ปจํ ์คํธ๋ช .Provider ์ปดํฌ๋ํธ๋ก ์ฌ์ฉํ ๊ณณ์ ์ง์ ํ๊ธฐexport default function MyApp() { const [theme, setTheme] = useState('light'); return ( <> <ThemeContext.Provider value={theme}> // value๋ก ํ ๋ง๊ฐ ์ค์ <Form /> </ThemeContext.Provider> // ๋คํฌ๋ชจ๋ ๋ผ์ดํธ๋ชจ๋ ์ ํํ๋ ๋ฒํผ <Button onClick={() => { setTheme(theme === 'dark' ? 'light' : 'dark'); }}> Toggle theme </Button> </> ) }
๐ useContext(์ฌ์ฉํ context) ๋ก ๋ถ๋ชจ์ context ๋ฌผ๋ ค๋ฐ๊ธฐfunction Form({ children }) { return ( <Panel title="Welcome"> <Button>Sign up</Button> <Button>Log in</Button> </Panel> ); } function Panel({ title, children }) { const theme = useContext(ThemeContext); // useContext ๋ถ๋ชจ context ๊ฐ์ ธ์ค๊ธฐ const className = 'panel-' + theme; return ( <section className={className}> <h1>{title}</h1> {children} </section> ) } function Button({ children, onClick }) { const theme = useContext(ThemeContext); // useContext ๋ถ๋ชจ context ๊ฐ์ ธ์ค๊ธฐ const className = 'button-' + theme; return ( <button className={className} onClick={onClick}> {children} </button> ); }
๐ ๊ฒฐ๊ณผToggle theme๋ฅผ ํด๋ฆญํ๋ฉด ๋คํฌ๋ชจ๋์ ๋ผ์ดํธ๋ชจ๋๋ฅผ ํ ๊ธ ํ์์ผ๋ก ๋ฐ๊ฟ ์ ์์ต๋๋ค.
๐ ์์ฝ
useContext hook์ ๋ํด์ ์์๋ณด์๋๋ฐ์! prop์ ๋ํ ์ดํด๊ฐ ์์๋ค๋ฉด ๊ธ์ ์ฝ๋ ๋ฐ ์ด๋ ค์์ด ์์ ์๋ ์์ต๋๋ค. ๋ค์์๋ prop์ ๋ํด์ ํฌ์คํ ํ๊ณ ๋งํฌ๋ฅผ ๊ฑธ์ด๋ฌ์ผ๊ฒ ์ด์! ์๋ฌดํผ context๋ prop์ ๋์์ผ๋ก ๋์๋ค๋ ๊ฒ๊ณผ ์ ์ญ ๋ณ์๋ฅผ ๊ด๋ฆฌํ๊ธฐ์ ์ข์ ์ ์ด๋ผ๋ ๊ฒ๋ง ๊ธฐ์ตํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์!
๋ค์์๋ ๋ ๋ค๋ฅธ ๋ฆฌ์กํธ ํ ์ ์์๋ณด๊ฒ ์ต๋๋ค!
์ฐธ๊ณ ๋ฌธ์
https://react.dev/reference/react/useContext#usageuseContext โ React
The library for web and native user interfaces
react.dev
https://react.dev/learn/passing-data-deeply-with-context
Passing Data Deeply with Context โ React
The library for web and native user interfaces
react.dev
LIST'JavaScript > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ