Last active
October 30, 2018 16:11
-
-
Save ajcrites/e46842c78c9f75e7bc596927edd92780 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
| class Toggle extends React.Component { | |
| static Consumer = ToggleContext.Consumer | |
| toggle = () => | |
| this.setState( | |
| ({on}) => ({on: !on}), | |
| () => this.props.onToggle(this.state.on), | |
| ) | |
| state = {on: false, toggle: this.toggle} | |
| render() { | |
| return ( | |
| <ToggleContext.Provider value={this.state} {...this.props} /> | |
| ) | |
| } | |
| } |
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
| function Toggle(props) { | |
| const [toggleState, setToggleState] = useState({ on: false }) | |
| const providerValue = { | |
| ...toggleState, | |
| toggle: () => | |
| setToggleState(({ on }) => { | |
| const isOn = !on; | |
| props.onToggle(isOn) | |
| return { on: isOn } | |
| }), | |
| } | |
| return <ToggleContext.Provider value={providerValue} {...props} /> | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment