Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have react-hooks/exhaustive-deps for a custom hook?

I wrote this hook (there could be bugs, I haven't used it yet):

import { useCallback, useEffect } from 'react';
import _throttle from 'lodash/throttle';

export function useThrottledCallback(cb, delay, ...deps) {
  const callback = useCallback(_throttle(cb, delay), deps);

  useEffect(() => {
    const lastCallback = callback;

    return () => lastCallback.cancel();
  }, [callback]);

  return callback;
}

Is there a way I can make the exhaustive-deps rule lint usages of this hook?

useThrottledCallback(() => (a + b + c)}, 100, [])

In this usage, I'd like to be notified that a, b, and c need to be in the dependency array.

like image 958
Brandon Avatar asked Jan 13 '21 17:01

Brandon


People also ask

What does React Hooks exhaustive DEPS do?

The "react-hooks/exhaustive-deps" rule warns us when we have a missing dependency in an effect hook. To get rid of the warning, move the function or variable declaration inside of the useEffect hook, memoize arrays and objects that change on every render or disable the rule.

Can we call Hooks from custom Hooks in React?

No. Custom Hooks are a mechanism to reuse stateful logic (such as setting up a subscription and remembering the current value), but every time you use a custom Hook, all state and effects inside of it are fully isolated.

Can we create custom Hooks in React?

Hooks are reusable functions. When you have component logic that needs to be used by multiple components, we can extract that logic to a custom Hook. Custom Hooks start with "use".

Can I use useContext in custom hook?

Create the useContextInside of this custom hook, we'll be using the useContext hook, that allows us to access both the theme and the setTheme function outside of this file. If useContext fails to create a context, it'll return undefined because we forgot the wrap our App or component in a ThemeProvider.

What are react hooks and how to use them?

Similarly, you can write custom Hooks to handle various use cases, such as form handling, animation, timers, and many others. The main advantage of React Hooks is the reusability of stateful logic. In addition, custom Hooks can be easily shared with other components without changing the component hierarchy.

Should a hook have a signature in react?

Just like in a component, make sure to only call other Hooks unconditionally at the top level of your custom Hook. Unlike a React component, a custom Hook doesn’t need to have a specific signature. We can decide what it takes as arguments, and what, if anything, it should return.

How do react hooks know which state corresponds to which usestate call?

As we learned earlier, we can use multiple State or Effect Hooks in a single component: So how does React know which state corresponds to which useState call? The answer is that React relies on the order in which Hooks are called. Our example works because the order of the Hook calls is the same on every render:

Are hooks stateful?

No. Custom Hooks are a mechanism to reuse stateful logic (such as setting up a subscription and remembering the current value), but every time you use a custom Hook, all state and effects inside of it are fully isolated.


1 Answers

It should be pretty easy. The documentation says:

exhaustive-deps can be configured to validate dependencies of custom Hooks with the additionalHooks option. This option accepts a regex to match the names of custom Hooks that have dependencies.

So you'd want something like:

{
  "rules": {
    // ...
    "react-hooks/exhaustive-deps": ["warn", {
      "additionalHooks": "useThrottledCallback"
    }]
  }
}
like image 79
CertainPerformance Avatar answered Oct 05 '22 22:10

CertainPerformance