Skip to content

Instantly share code, notes, and snippets.

@idmontie
Created April 7, 2018 02:32
Show Gist options
  • Save idmontie/365fdfb96267168871b7e6fe6b28a28d to your computer and use it in GitHub Desktop.
Save idmontie/365fdfb96267168871b7e6fe6b28a28d to your computer and use it in GitHub Desktop.
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