Skip to content

Instantly share code, notes, and snippets.

@mokargas
Created April 18, 2024 22:11
Show Gist options
  • Save mokargas/35a1a41b1435621d97112f91540fa0d9 to your computer and use it in GitHub Desktop.
Save mokargas/35a1a41b1435621d97112f91540fa0d9 to your computer and use it in GitHub Desktop.
Render props example
import React, {useState, useEffect} from 'react';
const MouseTracker = ({render}) =>{
const [position, setPosition] = useState({x:0, y:0});
useEffect(()=>{
const updatePosition = (e) =>{
setPosition({
x: e.clientX,
y: e.clientY,
})
}
window.addEventListener('mousemove', updatePosition);
return () => {
window.removeEventListener('mousemove', updatePosition);
}
});
return render(position)
}
export function App(props) {
return (
<div className='App'>
<h1>Hello React.</h1>
<h2>Start moving the mouse to see some magic happen!</h2>
<MouseTracker render={({x,y})=><p aria-live="polite">Mouse position:{x}, {y}</p>} />
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment