Exercício prático · React

Practice lab: Prevent a stale search result

do caderno de estudos · atualizado em 2 de outubro de 2026
Para quem vai encarar entrevista de React2 min

Tente resolver a tarefa antes de consultar os critérios. Compare o resultado e explique o que mudou no seu raciocínio.

Tarefa e critérios de verificação

Task, 15 minutes: build a local search component that requests results when its query changes. Simulate query A taking 300ms and query B taking 50ms. Issue B 10ms after A starts, so B finishes first. Predict the naive effect result, then ensure the latest query stays visible. Also check unmount cleanup and the development setup-cleanup-setup cycle. Include a display name derived from firstName and lastName without storing a second copy of that derived value.

Check your work:

  • After both requests settle, B's results remain visible. A cannot overwrite the newer result. Abort obsolete work or ignore its result using effect cleanup.
  • Unmounting and the development setup-cleanup-setup cycle leave no duplicate subscription or timer.
  • A full name derived from first and last name is calculated while rendering. It does not need a second state variable and an effect.

Follow-up: put editable rows in a list, edit one, then insert a row at the front. Verify that stable ids keep each row's state with the same item.

Source: React guidance on effects.

No app, a tarefa fica visível e você pode revelar os critérios depois da tentativa. Use as notas para registrar sua solução.

Praticar este exercício no app