iPod Video White

๐Ÿ““ TIL

[220728]

k_m_jin 2022. 7. 29. 11:21

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