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.

Feature suggestion: Allow custom list of hooks to ignore on exhaustive-deps

See original GitHub issue

The exhaustive-deps lint rule doesn’t require setters from useState, dispatch from useReducer, and refs from useRef in dependencies, because we know for sure those will never change. I’d like to be able to add additional custom hooks to that “safe list”. I understand this is potentially very dangerous, but we have a handful of hooks that simply wrap useRef with some logic. The returned result is essentially just a useRef result, so it’s perfectly safe to ignore as a dependency.

I’m imagining something like this in the eslint config:

{
  "rules": {
    // ...
    "react-hooks/exhaustive-deps": ["warn", {
      "safeHooks": "(useRefWrapper|useOtherRefWrapper)"
    }]
  }
}

Where useRefWrapper might look like so:

const useRefWrapper = () => {
  const ref = useRef();
  if (ref.current) {
    console.log('hi!');
  }
  return ref;
}

Does this seem like a reasonable option, assuming the documentation has a clear caveat mentioning the dangers? If so, I’m happy to work on implementation.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:31
  • Comments:5

github_iconTop GitHub Comments

13reactions
dharknesscommented, Mar 7, 2021

While a simple regex would work for some custom hooks, there are many that return arrays and objects that could also benefit from this.

const [ value, setValue ] = useRecoilState(atom)
               ^^^^^^^^
const { loading, data, error, reload } = useQuery(...)
                              ^^^^^^

If the config allows any form of data, each hook could specify stable values with an array: numbers for element indices or strings for object properties. Not sure what you’d assign to hooks like useRefWrapper() that return a single value. An empty array?

"stableHooks": {
  "useRefWrapper": [],
  "useRecoilState": [1],
  "useQuery": ["reload"]
}

Another option would be to pack each hook into an array similar to how other ESLint rules are configured. The hook name would go into the first element followed by the stable array elements or object properties or by itself if its single return value is stable.

"stableHooks": [
  ["useRefWrapper"],
  ["useRecoilState", 1],
  ["useQuery", "reload"]
]
5reactions
polRkcommented, Aug 30, 2021

Another example:

const dispatch = useDispatch();

useEffect(() => dispatch(fetchUsers()), []);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding the React exhaustive-deps linting warning
This ESlint plugin will ensure you are following the rules of Hooks in your React code, which are: Only call Hooks at the...
Read more >
Custom hooks with dependency lists and eslint-plugin ...
The react-hooks/exhaustive-deps rule allows you to check your custom ... This option accepts a regex to match the names of custom Hooks that ......
Read more >
Rules of Hooks
They let you use state and other React features without writing a class. Hooks ... Call Hooks from custom Hooks (we'll learn about...
Read more >
5 Tips to Help You Avoid React Hooks Pitfalls
The React Hooks feature was proposed in October 2018 and released ~4 ... who are annoyed by the exhaustive deps plugin, so let...
Read more >
You probably shouldn't ignore react-hooks/exhaustive- ...
It's tempting—we've all been there. We get the react-hooks/exhaustive-deps linting warning about a dependency array in a React hook.
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