Skip to content

Instantly share code, notes, and snippets.

@IanMitchell
Created December 26, 2016 04:31
Show Gist options
  • Save IanMitchell/71dc08c76211707e41d6f1d4dee8c706 to your computer and use it in GitHub Desktop.
Save IanMitchell/71dc08c76211707e41d6f1d4dee8c706 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