Faz parte do material de React que eu montei a partir das minhas entrevistas. Abaixo, a resposta em inglês e, quando existe, a pegadinha mais comum.
An Effect synchronizes a committed component with an external system. Return cleanup to release the resources it owns, such as a subscription or timer. Cleanup runs before a changed-dependency setup and on unmount. Effects do not run during server rendering.
With [], setup has no reactive dependencies. With [a, b], setup runs again after commits where either dependency changes by Object.is. Omitting the array runs setup after every commit of that component. Development Strict Mode can add a setup → cleanup → setup cycle to expose cleanup mistakes.
Trap: omitted dependencies can capture stale values. Include every reactive dependency or restructure the code so it is not needed. Derive display values during rendering, and handle user-triggered work in event handlers when appropriate.
Source: React useEffect.
Essa pergunta tem uma armadilha que derruba muita gente em entrevista. Ela está marcada na resposta acima: vale reler antes de treinar.
Ler é o primeiro passo. No app, essa pergunta vira flashcard com repetição espaçada, e o módulo tem quiz e simulado para testar o que ficou.
Treinar essa pergunta no app