Created
August 7, 2021 00:24
-
-
Save olalekanteeblaze/23de1a93ba232fe87a5ff3c006d1dc45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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