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.

exhaustive-deps: custom effects should support async functions

See original GitHub issue

We have a custom hook, useAsyncEffect, which works like useEffect except it accepts an async function. We would like to check the deps of this function using exhaustive-deps, but that lint rule is going a bit beyond its name and also checking the type of function we pass.

The exhaustive-deps rule can’t know anything about the semantics of arbitrary third-party hooks, so I think it’s overstepping its bounds a bit (at least if you take the name “exhaustive deps” literally)

React version: 16.13.1 eslint version: 7.0.0 eslint-plugin-react-hooks version: 4.0.3

Steps To Reproduce

Source:

import React from 'react';
import { useAsyncEffect } from './utilities/react';

function MyComponent() {
  useAsyncEffect(async () => {
    await Promise.resolve()
  }, []);
  return <div />;
}

.eslintrc.json:

{
  "parserOptions": {
    "ecmaVersion": 2020,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "plugins": ["react-hooks"],
  "rules": {
    "react-hooks/exhaustive-deps": [
      "error",
      {
        "additionalHooks": "^useAsyncEffect$"
      }
    ]
  }
}

Then run:

node_modules/.bin/eslint --ext jsx src/file.jsx

The current behavior

  5:18  error  Effect callbacks are synchronous to prevent race conditions. Put the async function inside:

<snip>

✖ 1 problem (1 error, 0 warnings)

The expected behavior

There should be no lint error

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:14
  • Comments:11

github_iconTop GitHub Comments

13reactions
Dawnthorncommented, Mar 13, 2021

I wonder if it would make more sense to move the effect callbacks async check into a separate rule like react-hooks/async-use-effect? In this case we want to check the dependencies, but we don’t want the async check. If they were separate rules we could configure them separately.

7reactions
cgriebelcommented, Jul 2, 2021

The suggestion to split them into 2 rules I think is ideal. It also allows suppressing one without the other when needed. I’m forced to decide between suppressing the false positive and risk dependencies being missed or having a bunch of warnings as it currently stands.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-hooks/exhaustive-deps warning--unclear about course ...
The issue here, and what the warning is pointing out, is that React hook callbacks are synchronous functions. You've passed an async function....
Read more >
Successfully using async functions in React useEffect
How to avoid the exhaustive deps ESLint error by properly using JavaScript async functions within the React useEffect Hook.
Read more >
Async Effects with Hooks - greatrexpectations
Use a custom hook to remove boilerplate in your async effects.
Read more >
The last guide to the useEffect Hook you'll ever need
First of all, you need to start thinking in effects. ... eslint-disable-next-line react-hooks/exhaustive-deps }, []);.
Read more >
Hooks FAQ - React
What can I do if my effect dependencies change too often? How do I implement ... Are Hooks slow because of creating functions...
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