Skip to content

Instantly share code, notes, and snippets.

@grantsheppard
Created April 6, 2017 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantsheppard/8742e1861081e47f78575b3aa55d3a4a to your computer and use it in GitHub Desktop.
Save grantsheppard/8742e1861081e47f78575b3aa55d3a4a to your computer and use it in GitHub Desktop.
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