Skip to content

Instantly share code, notes, and snippets.

@sabesansathananthan
Last active June 19, 2021 21:43
Show Gist options
  • Save sabesansathananthan/70e8b4ddcdc5e326dace8147912f8191 to your computer and use it in GitHub Desktop.
Save sabesansathananthan/70e8b4ddcdc5e326dace8147912f8191 to your computer and use it in GitHub Desktop.
How React Components Are Reused
<!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">
const {useState, useEffect} = React;
function useMouseLocation(location){
return (
<>
<h1>Please move the mouse here</h1>
<p>The current mouse position is: x:{location.x} y:{location.y}</p>
</>
);
}
function MouseTracker(props){
const [x, setX] = useState(0);
const [y, setY] = useState(0);
function handleMouseMove(event){
setX(event.clientX);
setY(event.clientY);
}
return (
<div onMouseMove={handleMouseMove}>
{useMouseLocation({x, y})}
</div>
)
}
ReactDOM.render(
<MouseTracker/>,
document.getElementById("root")
);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment