xnimorz/use-debounce

`callPending` should be wrapped in `useCallback`

Closed this issue · 2 comments

The documented example to use callPending when the components unmounts violates the principle of correctly defining the dependencies for useEffect.

Example code:

  // When the component goes to be unmounted, we will fetch data if the input has changed.
  useEffect(
    () => () => {
      callPending();
    },
    []
  );

Eslint rule violation:

React Hook useEffect has a missing dependency: 'callPending'. Either include it or remove the dependency array. eslint(react-hooks/exhaustive-deps)

Unfortunately, callPending has a new identity on every render, so listing it as a dependency won't achieve the desired result.

I feel the lib should memoize the returned callPending by using useCallback and then the example in the README should be updated to correctly follow the react-hooks/exhaustive-deps rule.

If you think this would make sense, I'm happy to submit a PR. Let me know :)

Hi @jfschwarz!
Good point :)
You are welcome to submit a PR, or I'll fix it later this week

Published in use-debounce@3.4.0