Skip to content

Instantly share code, notes, and snippets.

@chiefjester
Forked from IanMitchell/component.js
Created December 26, 2016 05:11
Show Gist options
  • Save chiefjester/05915870ad74b6980771f36bfb1ba9af to your computer and use it in GitHub Desktop.
Save chiefjester/05915870ad74b6980771f36bfb1ba9af to your computer and use it in GitHub Desktop.
React Events
export default class extends React.Component {
constructor(props) {
super(props)
this.state = { menuActive: false }
this.toggleNavigation = this.toggleNavigation.bind(this)
this.closeNavigation = this.closeNavigation.bind(this)
}
componentDidMount() {
document.body.addEventListener('click', this.closeNavigation)
}
componentWillUnmount() {
document.body.removeEventListener('click', this.closeNavigation)
}
toggleNavigation(e) {
console.log('Fires second')
this.setState({ menuActive: !this.state.menuActive })
e.preventDefault()
e.stopPropagation()
}
closeNavigation() {
if (this.state.menuActive) {
console.log('Fires first')
this.setState({ menuActive: false })
}
}
render () {
return (
<header>
<div className="header-top">
<a onClick={this.toggleNavigation}>
<h2>Menu</h2>
</a>
</div>
</header>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment