Render Props Mini
ReactJS
Hard
6 views
Problem Description
Create a component that provides mouse position using a render function prop.
Output Format
Render a React component.
Constraints
Use a function prop named render to output UI.
Official Solution
import React, { useState } from 'react';
export function MouseTracker({ render }) {
const [pos, setPos] = useState({ x: 0, y: 0 });
return (
<div
onMouseMove={(e) => setPos({ x: e.clientX, y: e.clientY })}
style={{ height: 160, border: '1px solid #eee', borderRadius: 14, padding: 12 }}
>
{render(pos)}
</div>
);
}
export default function App() {
return (
<MouseTracker
render={({ x, y }) => (
<div>
<strong>meetcode tracker</strong>
<div style={{ color: '#555' }}>x: {x}, y: {y}</div>
</div>
)}
/>
);
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!