Last active
June 19, 2021 21:44
-
-
Save sabesansathananthan/4942b83356d2cfc843ed5ef550ef0b9b to your computer and use it in GitHub Desktop.
How React Components Are Reused
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>React</title> | |
</head> | |
<body> | |
<div id="root"></div> | |
</body> | |
<script src="https://unpkg.com/react@17/umd/react.development.js"></script> | |
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> | |
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> | |
<script type="text/babel"> | |
class MouseTracker extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { x: 0, y: 0, } | |
} | |
handleMouseMove = (event) => { | |
this.setState({ | |
x: event.clientX, | |
y: event.clientY | |
}); | |
} | |
render() { | |
return ( | |
<div onMouseMove={this.handleMouseMove}> | |
{this.props.render(this.state)} {/* Render Props */} | |
</div> | |
) | |
} | |
} | |
class MouseLocation extends React.Component { | |
render() { | |
return ( | |
<> | |
<h1>Please move the mouse here</h1> | |
<p>The current mouse position is: x:{this.props.mouse.x} y:{this.props.mouse.y}</p> | |
</> | |
) | |
} | |
} | |
ReactDOM.render( | |
<MouseTracker render={mouse => <MouseLocation mouse={mouse} />}></MouseTracker>, | |
document.getElementById("root") | |
); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment