Skip to content

Instantly share code, notes, and snippets.

@choonkending
Created September 5, 2016 00:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choonkending/1fc594d9ffb3e5b288f26f3466123bce to your computer and use it in GitHub Desktop.
Save choonkending/1fc594d9ffb3e5b288f26f3466123bce to your computer and use it in GitHub Desktop.
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