useContext
- ์ปดํฌ๋ํธ ์ฌ์ฌ์ฉ์ด ์ด๋ ค์ ์ง๋ค.
- ์ ์ญ์ ์ธ ๋ฐ์ดํฐ ์ ๋ฌ
context ์์ฑ
//UserContext.js
import {createContext} from react
export const UserContext = createContext(context์ ์๋ฌด๊ฒ๋ ์ ๊ณต๋์ง ์์์๋ ์ฌ์ฉํ ๊ฐ)
context ์ data ์ ๊ณตํ๊ธฐ
//App.js
import {UserContext} from 'UserContext'
return (
<UserContext.Provider value={์ ๊ณตํ๊ณ ์ถ์ data}>
<์ปดํฌ๋ํธ>
</UserContext.Provider>
context ์ฌ์ฉ
import React, {useContext} from 'react'
import {UserContext} from 'UserContext'
const user = useContext(UserContext)
๋ฐ์ํ
'๐ TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[220731] React useMemo , useReducer (0) | 2022.07.31 |
---|---|
[220729] ๋ฆฌ์กํธ ์ค๊ฐ (0) | 2022.07.29 |
[220726] React (0) | 2022.07.27 |
[220725] (0) | 2022.07.26 |
[220720]team project (์ญ์ ๋ ์ฝ๋) /v-for (0) | 2022.07.20 |