Skip to content

Instantly share code, notes, and snippets.

@idmontie
Created April 8, 2018 02:32
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 idmontie/2ef9e4da23ffb61506c67500d9599409 to your computer and use it in GitHub Desktop.
Save idmontie/2ef9e4da23ffb61506c67500d9599409 to your computer and use it in GitHub Desktop.
const mousePosition = () => (BaseComponent) => {
return class MousePosition extends Component {
constructor(props) {
super(props);
this.state = { x: 0, y: 0 };
}
componentDidMount() {
this.listener = (e) => {
this.setState(() => ({ x: e.pageX, y: e.pageY }));
};
document.addEventListener('mousemove', this.listener);
}
componentWillUnmount() {
document.removeEventListener('mousemove', this.listener);
}
render() {
const { x, y } = this.state;
return <BaseComponent {...this.props} x={x} y={y} />;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment