3

I'm trying to debounce an onChange event when a user type in a input field.

I'm referencing these threads:

Perform debounce in React.js

Set input value with a debounced onChange handler

I have the following snippet where I try to replicate the solutions provided in the threads above:

  const handler = useCallback(debounce(setSearchQuery(value), 500), []);

  useEffect(() => {
    document.addEventListener('keydown', handleDocumentKeyDown);
    handler(value);
    return () => document.removeEventListener('keydown', handleDocumentKeyDown);
  }, [isOpen, handleDocumentKeyDown, handler, value]);

  ...

  const handleChange = (event) => {
    setValue(event.target.value);
  };

Error:

Uncaught TypeError: handler is not a function

How can I debounce setSerachQuery() for 500ms while the user is typing in the input field?

norbitrial
  • 13,852
  • 6
  • 27
  • 54
Carrein
  • 3,003
  • 3
  • 28
  • 67

1 Answers1

3

The issue in your case is that instead of passing a function to debounce, you are invoking it directly. You can use arrow function within debounce like

const handler = useCallback(debounce(() => setSearchQuery(value), 500), []);

Full code

const handler = useCallback(debounce(() => setSearchQuery(value), 500), []); // arrow function here

  useEffect(() => {
    document.addEventListener('keydown', handleDocumentKeyDown);
    handler(value);
    return () => document.removeEventListener('keydown', handleDocumentKeyDown);
  }, [isOpen, handleDocumentKeyDown, handler, value]);

  ...

  const handleChange = (event) => {
    setValue(event.target.value);
  };
Shubham Khatri
  • 246,420
  • 52
  • 367
  • 373
  • 8
    this gives me the lint error `React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead.eslintreact-hooks/exhaustive-deps` – Daniel Lizik Feb 24 '21 at 04:36
  • 2
    you could pass setSearchQuery as a dependency to useCallback but since the function doesn't change, you can disable the eslint warning too. Check [this post for more details](https://stackoverflow.com/questions/55840294/how-to-fix-missing-dependency-warning-when-using-useeffect-react-hook/55854902#55854902) – Shubham Khatri Feb 24 '21 at 05:28