Skip to content

Instantly share code, notes, and snippets.

@surajmandalcell
Last active October 29, 2019 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surajmandalcell/b5c08d98b69885badf1ea31b346c5c2e to your computer and use it in GitHub Desktop.
Save surajmandalcell/b5c08d98b69885badf1ea31b346c5c2e to your computer and use it in GitHub Desktop.
React context api example using hooks (function based components)
// 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