Created
April 7, 2018 02:32
-
-
Save idmontie/365fdfb96267168871b7e6fe6b28a28d 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
class MousePosition extends Component { | |
constructor(props) { | |
super(props); | |
this.onMouseMove = this._onMouseMove.bind(this); | |
this.state = { x: 0, y: 0 }; | |
} | |
componentDidMount() { | |
document.addEventListener('mousemove', this.onMouseMove); | |
} | |
_onMouseMove(e) { | |
this.setState(() => ({ | |
x: e.clientX, | |
y: e.clientY, | |
})); | |
} | |
render() { | |
const { x, y } = this.state; | |
const { children } = this.props; | |
return children({ x, y }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment