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.

Add lint rule for useRecoilCallback deps array

See original GitHub issue

Custom hooks that have custom dependency arrays can’t be statically analyzed by the react-hooks linter rule, and can’t have their dependencies automatically tracked. Because of this, I’d suggest either:

  1. Accept a callback and add that callback to the deps array of the internal useCallback. It’ll make the callback on each render, or if the passed callback is useCallback’d, whenever the passed callback is recreated

  2. Use the ‘effect ref’ pattern, so the end user can always pass a flat callback, but the callback function doesn’t have to be a part of the deps array:

    function useRecoilCallback(fn) {
      const fnRef = useRef(fn)
      useEffect(() => {
        fnRef.current = fn
      })
    
      return useCallback(() => {
        // read fnRef.current instead of fn
      }, [some, deps])
    }
    

I think 1. is “more correct” and would result in less bugs both internally and for the end user, but 2. would result in better UX, since 1. would require the user to useCallback/useMemo themselves if they wanted a properly recreated callback. A note in the docs about that might help, but those are fairly easy to miss 😅

Either way, I think this change will give a better UX when working with the hooks lint rule, with less surprises in general

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
acutmorecommented, May 30, 2020

It looks like customisation is already available:

https://github.com/facebook/react/blob/9752d31f127037e8126177b0456ab1b0547eb2db/packages/eslint-plugin-react-hooks/README.md#advanced-configuration

// .eslint.js
module.exports = {
  rules: {
    // ...
    "react-hooks/exhaustive-deps": [
      "warn",
      {
        additionalHooks: "useRecoilCallback",
      },
    ],
  },
};

A downside is that each project will separately need to remember to add this. Having this packaged up so it’s a one line recommended eslint plugin might be less typo prone.

2reactions
jaredpalmercommented, May 30, 2020

@acutmore we should just add this to getting started/installation instructions. seems totally reasonable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding the React exhaustive-deps linting warning
Adding the exhaustive deps lint rule and the rule of Hooks rule is a necessity for app development. These rules will save you...
Read more >
useRecoilCallback(callback, deps) | Recoil
useRecoilCallback (callback, deps) · Asynchronously read Recoil state without subscribing a React component to re-render if the atom or selector is updated.
Read more >
Understanding the React Hooks 'exhaustive-deps' lint rule
It requires me to add onChange to the useEffect dependencies array. But in my understanding onChange will never change, so it should not...
Read more >
The exhaustive-deps rule has to be treated seriously
We intend to just run some code when X changes, but then ESLint tells us to add Y and Z to the dependency...
Read more >
@webiny/i18n-react | Yarn - Package Manager
... api-wcp: add node-fetch dep (bdff039); pulumi-aws: add dynamodb query action to router policy (d2f0c78); pulumi-aws: support array of domain strings ...
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