Guest User

Untitled

a guest
Jul 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. import React from 'react'
  2.  
  3. const ThemeContext = React.createContext({
  4. color: 'orange',
  5. toggleColor() {},
  6. })
  7.  
  8. class Theme extends React.Component {
  9. state = {
  10. color: 'orange',
  11. toggleColor: () => {
  12. this.setState({
  13. color: this.state.color === 'orange' ? 'purple' : 'orange',
  14. })
  15. },
  16. }
  17.  
  18. render() {
  19. return (
  20. <ThemeContext.Provider value={this.state}>
  21. {this.props.children}
  22. </ThemeContext.Provider>
  23. )
  24. }
  25. }
  26.  
  27. const Button = props => (
  28. <ThemeContext.Consumer>
  29. {({ color, toggleColor }) => (
  30. <button style={{ color }} onClick={toggleColor}>
  31. {props.text}
  32. </button>
  33. )}
  34. </ThemeContext.Consumer>
  35. )
  36.  
  37. const App = () => (
  38. <Theme>
  39. <Button text="hey!" />
  40. </Theme>
  41. )
Add Comment
Please, Sign In to add comment