Created
February 5, 2018 16:26
-
-
Save JoaoCnh/a5c4766836b8851edf314f83998dd4a5 to your computer and use it in GitHub Desktop.
Mouse Render Prop
This file contains hidden or 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 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> | |
); | |
} | |
} | |
// Easy to use! | |
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