React · Middle · Question 75 of 75
Why can useEffect with an async callback be a problem?
useEffect(async () => {
setData(await load());
}, []);- ✓Correct answer: The effect function must return a cleanup function or nothing; an async function returns a promise
- 2Effects can't use
awaitanywhere - 3
loadwould run twice
Explanation
Define an async function inside the effect and call it: useEffect(() => { let ignore = false; (async () => { ... })(); return () => { ignore = true }; }, []).