Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
Lodash throttle not working correctly in React

I have a function that filters through some state and renders out the result for a search request.

  const handleSearch = (value: string) => {
   const searchResultData = users.filter((userId) => user.id.startsWith(value));
   setSearchResult(searchResultData);
  };

I am trying to work with lodash.throttle library to cause a delay before the request is sent. So we don't have a request go out every time a user types.

 const handleSearch = useCallback(throttle((value: string) => {
  const searchResultData = users.filter((userId) => user.id.startsWith(value));
  setSearchResult(searchResultData);
 }, 2500), []);

This works in delaying input as expected but for some reason, the user.filter method doesn't run, and so the state isn't updated with the search result. I believe the problem might be from the useCallback hook, but the throttle function is dependent on it to run. Any ideas on how I can work around this problem?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If your throttled/debounced handler uses props or state, like this:

const { fetcherFunctionFromProps } = props;

const eventHandler = async () => {
  const resp = await fetcherFunctionFromProps();
};    

const debouncedEventHandler = useMemo(
  () => throttle(eventHandler, 300)
), [fetcherFunctionFromProps]);

And it doesn't work,
you can refactor it to the following:

const { fetcherFunctionFromProps } = props;

const eventHandler = async (fetcher) => {
  const resp = await fetcher();
};

const debouncedEventHandler = useMemo(() => throttle(eventHandler, 300), []);
...
<Component onClick={() => debouncedEventHandler(fetcherFunctionFromProps)}>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!