Created
October 10, 2022 01:15
-
-
Save nijjwal/da872782ce7fa9e1d3b06576f749e9bf to your computer and use it in GitHub Desktop.
useContext Hook # 3 sample code
This file contains 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 { useContext, createContext } from "react"; | |
const moods = { | |
happy: ':)', | |
sad: ':(' | |
}; | |
const MoodContext = createContext(moods); | |
function App() { | |
return ( | |
<MoodContext.Provider value={moods.sad}> | |
<MoodEmoji/> | |
</MoodContext.Provider> | |
); | |
} | |
function MoodEmoji(){ | |
const mood = useContext(MoodContext); | |
return <p>{mood}</p> | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment