I want to use the async/await pattern in my useEffect
React hook and I'm getting an ESLint warning that I don't understand.
Here is my code that causes the warning
useEffect(async () => {
const delay = () => new Promise((resolve) => setTimeout(resolve, 1000));
async function getData() {
await delay();
}
getData();
}, []);
and the warning:
ESLint: Effect callbacks are synchronous to prevent race conditions. Put the async function inside: (react-hooks/exhaustive-deps)
The answer from a similar question says to add useCallback
, but as I have no dependencies, it does not seem to help.
How to fix missing dependency warning when using useEffect React Hook
Here is what that incorrect answer looks like:
const delay = () => new Promise((resolve) => setTimeout(resolve, 1000));
async function getData() {
await delay();
}
getData();
},[]), []);