Skip to content

Instantly share code, notes, and snippets.

@hg-pyun
Created June 13, 2018 07:35
Show Gist options
  • Save hg-pyun/7dced6056f14917b50f7a0683c20d8d5 to your computer and use it in GitHub Desktop.
Save hg-pyun/7dced6056f14917b50f7a0683c20d8d5 to your computer and use it in GitHub Desktop.
class Cat extends React.Component {
render() {
const mouse = this.props.mouse;
return (
<img src="/cat.jpg" style={{ position: 'absolute', left: mouse.x, top: mouse.y }} />
);
}
}
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}>
{/*
Instead of providing a static representation of what <Mouse> renders,
use the `render` prop to dynamically determine what to render.
*/}
{this.props.render(this.state)}
</div>
);
}
}
class MouseTracker extends React.Component {
render() {
return (
<div>
<h1>Move the mouse around!</h1>
<Mouse render={mouse => (
<Cat mouse={mouse} />
)}/>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment