Capítulo 53 de 116
useEffect(setup, dependencies?) is the API reference behind the conceptual model built up in Ch 36/38: setup runs after commit, may return a cleanup function, and re-runs whenever a listed dependency changes between renders — this chapter is the precise parameter/timing contract.
useEffect(setup, dependencies?). setup is the function React runs after the DOM updates; if it returns a function, that's the cleanup, run before the next setup call and on unmount.[] → runs once after initial mount only; [a, b] → runs after mount and after any render where a or b differs from the previous render (compared with Object.is).useLayoutEffect (Ch 58), which runs synchronously before paint.setup/cleanup must be synchronous functions (an Effect callback itself can't be async, though it can call async functions inside); in Strict Mode development builds, React runs setup → cleanup → setup once extra on mount specifically to surface missing/incorrect cleanup; if a dependency is an object/function recreated every render, the Effect re-runs every render unless that's addressed (Ch 40).useEffect is for staying synchronized with something external as long as the component (and its dependency values) are present, not for reacting to one specific interaction.useEffect(() => {
document.title = `${unreadCount} unread messages`;
}, [unreadCount]);
unreadCount), re-running only when that value changes.setup did, so repeated mount→cleanup→mount cycles (Strict Mode, or genuine remounts) never leak or duplicate a subscription/connection.