Skip to content

Instantly share code, notes, and snippets.

@olalekanteeblaze
Created August 7, 2021 00:20
Show Gist options
  • Save olalekanteeblaze/e04a60f871e27d4caa741f09038b0330 to your computer and use it in GitHub Desktop.
Save olalekanteeblaze/e04a60f871e27d4caa741f09038b0330 to your computer and use it in GitHub Desktop.
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