Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

294
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda