useEffect

The React hook for running side work, such as adding listeners or changing document.body, after a component renders.

In more detail

An effect can return a cleanup function that React runs before the effect runs again and when the component unmounts. Overlays use it to lock scroll and set inert, and the cleanup restores the previous values.

Example

useEffect(() => { const id = setInterval(tick, 1000); return () => clearInterval(id); }, []);

Related terms

Taught in

References