useEffect is a React Hook used to synchronize a component with external systems after rendering. It also replaces some class lifecycle methods and Vue options-API patterns, making its timing important in interviews and UI debugging. section 1.4 section 4.4
Tooltip positioning
A tooltip can measure its anchor with ref.current.getBoundingClientRect() inside useEffect, then update its position. Because useEffect runs after the browser paints, the tooltip may briefly appear at {top: 0, left: 0} and flicker. Replace it with useLayoutEffect when the measurement must be applied synchronously after DOM mutations but before painting. section 4.4
Interview checks include:
- Explain that flickering may disappear on fast machines or lightweight UIs, so simulate slow rendering or add an artificial delay to reproduce it. section 4.4
- Distinguish post-paint useEffect from pre-paint useLayoutEffect rather than treating them as interchangeable lifecycle tools. section 4.0 section 4.4
Listing 4.7 Example of Tooltip component
const Tooltip: React.FC<TooltipProps> = ({ text, ref }) => {
const [position, setPosition] = useState<Position>({ top: 0, left: 0 });
useEffect(() => {
if (ref.current) {
const rect = ref.current.getBoundingClientRect();
setPosition({
top: rect.bottom + window.scrollY,
left: rect.left + window.scrollX,
});
}
}, [ref]);
return (
<div
style={{
position: 'absolute',
...position,
background: 'white',
border: '1px solid blue',
padding: '5px',
}}
>
{text}
</div>
);
};
Inspect the tooltip implementation that demonstrates DOM measurement, state updates, and the timing-sensitive Hook choice. section 4.4




