Created
February 11, 2019 20:08
-
-
Save davidcostadev/93eae595a06c6381ada6b232d85c34bd to your computer and use it in GitHub Desktop.
Toggle
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 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