-
-
Save chiefjester/05915870ad74b6980771f36bfb1ba9af to your computer and use it in GitHub Desktop.
React Events
This file contains 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
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