Skip to content

QuizNot attempted

Async work in useEffect

Why is useEffect(async () => { setData(await load()); }, []) wrong, and what is the fix?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • An async function returns a Promise, but the effect must return nothing or a cleanup function; define an async function inside the effect and call it
  • React treats the effect's return value as its cleanup, and an async function always returns a Promise (TypeScript flags it too)
  • define an async function inside the effect, call it, and return a real cleanup that aborts or ignores late results.
  • Not 'effects can't setState': they can.
  • Not 'await not allowed in .tsx': it is allowed inside async functions.
  • Not 'React awaits the promise': React ignores it, and an unhandled late response can set state after unmount.