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.

How to disable the rule react-hooks/exhaustive-deps?

See original GitHub issue

I’m trying to disable the rule react-hooks/exhaustive-deps. I’ve tried adding "react-hooks/exhaustive-deps": "off", to .eslintrc without any luck.

How do I disable this rule? Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:68 (15 by maintainers)

github_iconTop GitHub Comments

24reactions
WesCossickcommented, Apr 24, 2019

For us, one of the primary places we’re receiving this warning is for useEffect hooks that we only want to run when the component mounts, and never again for that component.

As a concrete example, the app we’re working on can display a small notification in the top right corner that automatically disappears after 15 seconds. Here’s a super trimmed down version of what that might look like:

// Imports
import React, { useEffect } from 'react';


// Export component
export default (props) => {
    // Handle hiding this notification
    const hideSelf = () => {
        // In our case, this simply dispatches a Redux action
    };
    
    
    // Automatically hide the notification
    useEffect(() => {
        setTimeout(() => {
            hideSelf();
        }, 15000);
    }, []);
    
    
    // Return JSX
    return (
        <div className='notification'>
            Just an example <button onClick={hideSelf}>Close button</button>
        </div>
    );
};

This complains because hideSelf is not a dependency listed in the useEffect hook. But that’s intentional; we only want that hook to run when this component mounts.

Is there a better way to design this? Or would this be a false positive?

23reactions
raRaRacommented, Apr 23, 2019

The empty brackets very clearly indicate that the effect only runs when the component mounts

This is one of the reasons I don’t want to add “unnecessary” things to my deps, as it makes the code look more complex and confusing. I totally agree that empty brackets tell you that it’s CDM. There are many cases in my app where I just want to fetch once when the component mounts, but now I need to choose whether I want to add bunch of // eslint-disable-next-line react-hooks/exhaustive-deps lines or add the props as deps, but then I need to make sure the effect doesn’t run any code when one of the deps changes. @gaearon I believe this is the main problem with this rule or hook. Perhaps we just need more hooks or options?

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - How do I configure eslint rules to ignore react-hooks ...
Usually you don't want to disable the rule. However, there are few cases where it's worthwhile to disable it. For example, if you're...
Read more >
You probably shouldn't ignore react-hooks/exhaustive-deps ...
We have a seemingly-easy out: we can ignore the linting error by placing a // eslint-disable-next-line right above the dependency array. Here's ...
Read more >
Rules of Hooks - React
By following this rule, you ensure that Hooks are called in the same order each time a component renders.
Read more >
Understanding the React exhaustive-deps linting warning
What is the exhaustive deps lint rule? · count to the dependency array or removing the dependency array altogether. If you remove the...
Read more >
How bad is disabling `react-hooks/exhaustive-deps`? - Reddit
How bad is disabling `react-hooks/exhaustive-deps`? ... Hello! I learning react and I try to create some useEffects (useInit - run once, useHttp - ......
Read more >

github_iconTop Related Medium Post

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