import React , {useContext} from 'react'
const UseContext = ()=>{
const UseContextCon = React.createContext({})
const Son = () =>{
const {name} = useContext(UseContextCon)
return (
<p>我是son组件 我的名字是{name}</p>
)
}
const Child = () =>{
const {name} = useContext(UseContextCon)
return (
<p>我是child组件 我的名字是{name}</p>
)
}
return (
<UseContextCon.Provider value={{name:'context'}}>
<Son />
<Child/>
</UseContextCon.Provider>
)
}
export default UseContext
(精华2020年5月25日更新) react基础篇 react-hooks的useContext用法
猜你喜欢
转载自blog.csdn.net/weixin_41181778/article/details/106324276
今日推荐
周排行