Created
August 7, 2021 00:20
-
-
Save olalekanteeblaze/e04a60f871e27d4caa741f09038b0330 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, Component } 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 /> | |
) | |
} | |
class ThirdLevelChild extends Component { | |
render ( | |
<div>`My name is ${this.context.name}`</> | |
) | |
} | |
ThirdLevelChild.contextType = NameContext |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment