Created
April 6, 2017 04:24
-
-
Save grantsheppard/8742e1861081e47f78575b3aa55d3a4a to your computer and use it in GitHub Desktop.
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
const withMousePosition = Component => class extends React.Component { | |
state = { | |
mouse: { x: 0, y: 0 } | |
} | |
onMouseMove = e => { | |
this.setState({ mouse: { x: e.clientX, y: e.clientY }}) | |
} | |
componentDidMount() { | |
window.addEventListener('mousemove', this.onMouseMove) | |
} | |
componentWillUmount() { | |
window.removeEventListener('mousemove', this.onMouseMove) | |
} | |
render() { | |
return <Component mouse={this.state.mouse} {...this.props} /> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment