Skip to content

Instantly share code, notes, and snippets.

@deminoth
Last active May 1, 2018 04:46
Show Gist options
  • Save deminoth/9faa07db3fda83c2d237fecb3902aebb to your computer and use it in GitHub Desktop.
Save deminoth/9faa07db3fda83c2d237fecb3902aebb to your computer and use it in GitHub Desktop.
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