Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useThrottledCallback doesn't consider dependencies #386

Closed
jebae opened this issue Jul 11, 2022 · 0 comments
Closed

useThrottledCallback doesn't consider dependencies #386

jebae opened this issue Jul 11, 2022 · 0 comments

Comments

@jebae
Copy link

jebae commented Jul 11, 2022

Describe the bug
It seems useThrottledCallback doesn't consider dependencies array.

To Reproduce
I used useThrottledCallback with dependency mode.

const handleMouseMove = useThrottledCallback(
  event => {
    if (mode === horizontal) {
      handleHorizontal(event);
    } else {
      handleVertical(event);
    }
  },
  [mode], // dependencies
  16, // throttled time
);

If mode change, callback(first parameter) must change inside useThrottledCallback, but it doesn't.

Expected behavior
Expected to consider variable inside dependencies. I guess there is one problem in useThrottledCallback. Here is source.

const useThrottledCallback = <TCallback extends GenericFunction>
  (fn: TCallback, dependencies?: any[], wait: number = 600, options: ThrottleSettings = defaultOptions) => {
  const throttled = useRef(throttle<TCallback>(fn, wait, options))

  useEffect(() => {
    throttled.current = throttle(fn, wait, options)
  }, [fn, wait, options])

  useWillUnmount(() => {
    throttled.current?.cancel()
  })

  return useCallback(throttled.current, dependencies)
}

Callback would be saved in ref. And as docs said, it is wrapped by useCallback. But what happen if dependencies changes? throttled.current may not change, because it only changes when [fn, wait, options] change with effect hook.

It seems memoization does not consider dependencies. Please let me know if it's wrong. Anyway useCallback works, but I want awesome useThrottledCallback that u guys made 👍

Desktop (please complete the following information):

  • OS: macos bigsur
  • Browser chrome
  • Version 1.2.0
@jebae jebae closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant