Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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