Last active
May 1, 2018 04:46
-
-
Save deminoth/9faa07db3fda83c2d237fecb3902aebb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 from 'react'; | |
const ThemeContext = React.createContext('light'); | |
class App extends React.Component { | |
render() { | |
return ( | |
<ThemeContext.Provider value="dark"> | |
<Toolbar /> | |
</ThemeContext.Provider> | |
); | |
} | |
} | |
function Toolbar(props) { | |
return ( | |
<div> | |
<ThemedButton /> | |
</div> | |
); | |
} | |
function withTheme(Component) { | |
return function ThemedComponent(props) { | |
return ( | |
<ThemeContext.Consumer> | |
{theme => <Component {...props} theme={theme} />} | |
</ThemeContext.Consumer> | |
); | |
}; | |
} | |
function Button({theme, ...rest}) { | |
return <button className={theme} {...rest} />; | |
} | |
const ThemedButton = withTheme(Button); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment