Skip to content

Instantly share code, notes, and snippets.

@hg-pyun
Created June 13, 2018 07:17
Show Gist options
  • Save hg-pyun/0c728fd9605f69deea436ff20988bce6 to your computer and use it in GitHub Desktop.
Save hg-pyun/0c728fd9605f69deea436ff20988bce6 to your computer and use it in GitHub Desktop.
// The <Mouse> component encapsulates the behavior we need...
class Mouse extends React.Component {
constructor(props) {
super(props);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.state = { x: 0, y: 0 };
}
handleMouseMove(event) {
this.setState({
x: event.clientX,
y: event.clientY
});
}
render() {
return (
<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
{/* ...but how do we render something other than a <p>? */}
<p>The current mouse position is ({this.state.x}, {this.state.y})</p>
</div>
);
}
}
class MouseTracker extends React.Component {
render() {
return (
<div>
<h1>Move the mouse around!</h1>
<Mouse />
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment