Skip to content

Instantly share code, notes, and snippets.

@davidcostadev
Created February 11, 2019 20:08
Show Gist options
  • Save davidcostadev/93eae595a06c6381ada6b232d85c34bd to your computer and use it in GitHub Desktop.
Save davidcostadev/93eae595a06c6381ada6b232d85c34bd to your computer and use it in GitHub Desktop.
Toggle
import React from 'react';
// Toggle.js
class Toggle extends React.Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
toggleOn: false,
toggleOff: true,
};
}
toggle() {
const { toggleOn, toggleOff } = this.state;
this.setState({
toggleOn: !toggleOn,
toggleOff: !toggleOff,
});
}
render() {
const { toggleOn, toggleOff } = this.state;
return this.props.render({ toggleOn, toggleOff, toggle: this.toggle });
}
}
// example
const Header = () => (
<Toggle
render={({ toggleOn, toggle }) => (
<div className={classname({ open: toggleOn })}>
<Menu onToggle={toggle} />
</div>
)}
/>
);
export default Header;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment