Last active
February 6, 2019 15:05
-
-
Save gisderdube/96b5072f67338a613c861891b576635a to your computer and use it in GitHub Desktop.
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 React, { useContext } from 'react' | |
import Counter from './Counter' | |
import { ColorContext, ColorContextProvider } from './ColorContext' | |
function Application() { | |
return ( | |
<ColorContextProvider> | |
<Workspace /> | |
</ColorContextProvider> | |
) | |
} | |
function Workspace() { | |
const { color, toggleColor } = useContext(ColorContext) | |
return ( | |
<div> | |
<h1 style={{ color }}>Making sense of React Hooks</h1> | |
<Counter /> | |
<button onClick={toggleColor}>Toggle color</button> | |
</div> | |
) | |
} | |
export default Application |
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 React, { useState } from 'react' | |
export const ColorContext = React.createContext() | |
export function ColorContextProvider({ children }) { | |
const [color, setColor] = useState('#35CE8D') | |
const toggleColor = () => { | |
color === '#35CE8D' ? setColor('#721817') : setColor('#35CE8D') | |
} | |
const value = { color, toggleColor } | |
return <ColorContext.Provider value={value}>{children}</ColorContext.Provider> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment