Last active
October 29, 2019 04:28
-
-
Save surajmandalcell/b5c08d98b69885badf1ea31b346c5c2e to your computer and use it in GitHub Desktop.
React context api example using hooks (function based components)
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
// https://youtu.be/6uBgda52yEo | |
import React, {useState, setState } from 'react'; | |
import { render } from 'react-dom'; | |
export const FruitContext = React.createContext('Apple'); | |
const Index = () => { | |
const [fruit, setFruit] = useState('Apple'); | |
return ( | |
<FruitContext.Provider value={[fruit, setFruit]}> | |
<App /> | |
</FruitContext.Provider> | |
); | |
} | |
export const App = () => { | |
const [fruit, setFruit] = useContext(FruitContext); | |
render( | |
<div> | |
<h1>React Hooks In Context</h1> | |
<h1>Today's Fruit is {fruit}</h1> | |
</div> | |
) | |
} | |
render (<Index />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment