Skip to content

Instantly share code, notes, and snippets.

@olalekanteeblaze
Created August 7, 2021 00:24
Show Gist options
  • Save olalekanteeblaze/23de1a93ba232fe87a5ff3c006d1dc45 to your computer and use it in GitHub Desktop.
Save olalekanteeblaze/23de1a93ba232fe87a5ff3c006d1dc45 to your computer and use it in GitHub Desktop.
import React, { useState, useContext } from 'react'
export const NameContext = React.createContext()
export const NameContextProvider = ({ children }) => {
const [name, setName] = useState('Moshood')
return (
<NameContext.Provider value={{ name, setName }}>
{children}
</NameContext.Provider>
)
}
function App () {
return (
<NameContextProvider>
<Parent />
</NameContextProvider>
)
}
function Parent () {
return (
<FirstLevelChild />
)
}
function FirstLevelChild (){
return (
<SecondLevelChild />
)
}
function SecondLevelChild (){
return (
<ThirdLevelChild />
)
}
function ThirdLevelChild () {
const value = useContext(MyContext)
return (
<div>`My name is ${value.name}`</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment