[eslint-plugin-react-hooks] async functions should be allowed for custom effects
See original GitHub issueConsider the following code:
useAsyncEffect(async () => {
await foo()
}, []);
Eslint give the following error:
11:18 error Effect callbacks are synchronous to prevent race conditions. Put the async function inside:
useEffect(() => {
async function fetchData() {
// You can await here
const response = await MyAPI.getData(someId);
// ...
}
fetchData();
}, [someId]); // Or [] if effect doesn't need props or state
There are 2 problems with that:
- The error is provided by
react-hooks/exhaustive-deps
, but this has nothing to do with deps or exhaustiveness. I can’t disable this warning without disable actually checking for exhaustive deps. - In general the error is bogus. Statement “Effect callbacks are synchronous to prevent race conditions” is only true for built-in effects, and is definitely not true for
useAsyncEffect
, which built specifically to support asynchronous callbacks.
eslint-plugin-react-hooks version: 4.0.0
The current behavior
react-hooks/exhaustive-deps
gives an error for this code
The expected behavior
react-hooks/exhaustive-deps
should not give an error for this code
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:9 (5 by maintainers)
Top Results From Across the Web
React Hook Warnings for async function in useEffect
Using a self invoking function not let async leak to the useEffect function definition or a custom implementation of a function that triggers ......
Read more >Hooks FAQ - React
Importantly, custom Hooks give you the power to constrain React API if you'd like to type ... The calls to act() will also...
Read more >How to use async functions in useEffect (with examples)
Using asynchronous functions in a useEffect hook is quite common, notably for data fetching. Let's see several ways of going about it!
Read more >5 Mistakes to Avoid When Using React Hooks - Dmitri Pavlutin
In this post, I will describe the React hooks usage mistakes, and how to fix them. ... useEffect() hook fetches the game information...
Read more >Eslint Error In React-Native After New Module Add(Await/Async)
[eslint-plugin-react-hooks] async functions should be allowed for custom effects #18858. no-async-promise-executor. - Disallow using an async function as a ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Technically, I could, I’ve thought of that. But
useAsyncEffect
is the obvious name for what it does. I don’t want to choose objectively worse name because of a linter rule.https://github.com/facebook/react/blob/master/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js#L104 is where the check happens.
iiuc, this is to allow passing back a cleanup/cancel function, which is much harder with your abstraction. I hope you’ve considered those ramifications and have your own workarounds.
I dunno whether the team will change the regex, would be interested to hear from them.