question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[eslint-plugin-react-hooks] async functions should be allowed for custom effects

See original GitHub issue

Consider 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:

  1. 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.
  2. 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
vkurchatkincommented, May 7, 2020

An Effect has a specific shape and meaning in React, so maybe co-opting it might not be the right move.

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.

1reaction
threepointonecommented, May 7, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found