Skip to content

Instantly share code, notes, and snippets.

@nijjwal
Created October 10, 2022 01:15
Show Gist options
  • Save nijjwal/da872782ce7fa9e1d3b06576f749e9bf to your computer and use it in GitHub Desktop.
Save nijjwal/da872782ce7fa9e1d3b06576f749e9bf to your computer and use it in GitHub Desktop.
useContext Hook # 3 sample code
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