Brian Vaughn
7 years ago
4 changed files with 54 additions and 1 deletions
@ -0,0 +1,10 @@ |
|||||
|
const ThemeContext = React.createContext('light'); |
||||
|
|
||||
|
function ThemedButton(props) { |
||||
|
// highlight-range{2-4}
|
||||
|
return ( |
||||
|
<ThemeContext.Consumer> |
||||
|
{theme => <button className={theme} {...props} />} |
||||
|
</ThemeContext.Consumer> |
||||
|
); |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
function Button({theme, ...rest}) { |
||||
|
// highlight-next-line
|
||||
|
return <button className={theme} {...rest} />; |
||||
|
} |
||||
|
|
||||
|
// highlight-next-line
|
||||
|
const ThemedButton = withTheme(Button); |
@ -0,0 +1,18 @@ |
|||||
|
const ThemeContext = React.createContext('light'); |
||||
|
|
||||
|
// This function takes a component...
|
||||
|
// highlight-next-line
|
||||
|
export function withTheme(Component) { |
||||
|
// ...and returns another component...
|
||||
|
// highlight-next-line
|
||||
|
return function ThemedComponent(props) { |
||||
|
// ... and renders the wrapped component with the context theme!
|
||||
|
// Notice that we pass through any additional props as well
|
||||
|
// highlight-range{2-4}
|
||||
|
return ( |
||||
|
<ThemeContext.Consumer> |
||||
|
{theme => <Component {...props} theme={theme} />} |
||||
|
</ThemeContext.Consumer> |
||||
|
); |
||||
|
}; |
||||
|
} |
Loading…
Reference in new issue