Created
September 5, 2016 00:18
-
-
Save choonkending/1fc594d9ffb3e5b288f26f3466123bce 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, { Component, PropTypes } from 'react'; | |
export default class Toggle extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { isActive: false }; | |
this.onToggle = this.onToggle.bind(this); | |
} | |
render() { | |
return ( | |
this.props.children(this.state.isActive, this.onToggle) | |
); | |
} | |
onToggle() { | |
this.setState({ | |
isActive: !this.state.isActive | |
}); | |
} | |
} | |
// How to use | |
const ToggleConsumer = props => ( | |
<Toggle> | |
{ | |
(isActive, onToggle) => { | |
return ( | |
<div> | |
<Title onClick={onToggle} /> | |
{ isActive && <Content /> } | |
</div> | |
); | |
} | |
} | |
</Toggle> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment