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

95
Visualizações
Debouncing on OnChange Event - ReactJS

I have a search form which is changing my params on my page. It's actually working fine, however whenever you input any character at all, it will automatically refresh the page and re-fetch results. This is not what I want.

Is there a better way to action this, e.g debounce the search results for an idle time on the user input OR dynamically fetch?

See my code below:

  function handleParamChange(e) {
    const param = e.target.name //name may be desc
    const value = e.target.value
    setParams(prevParams => {
      return { ...prevParams, [param]: value}
    })
  }
<SearchForm params={params} onParamChange={handleParamChange}/>

This will then be fed to component as such.

const SearchForm = ({params, onParamChange}) => {

  return (
    <GeneralFilterContainer>
        <TextField id="standard-basic" 
                   label="Search Roles" 
                   variant="standard"  
                   size="small"
                   onChange={onParamChange}
                   value={params.what}
                   name="what"
                   type="text"
                   />
    </GeneralFilterContainer>
  )
}

export default SearchForm

As above, i just want to debounce and dynamically fetch rather then it just reload every time.

Regards

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The onChange event gets fired after each key the user types, so yes, your app in this case will reload after each keypress. A simple solution woulb be to add this function in your component:

function debounce(func, wait, immediate) {
  var timeout;

  return function executedFunction() {
    var context = this;
    var args = arguments;
        
    var later = function() {
      timeout = null;
      if (!immediate) func.apply(context, args);
    };

    var callNow = immediate && !timeout;
    
    clearTimeout(timeout);

    timeout = setTimeout(later, wait);
    
    if (callNow) func.apply(context, args);
  };
};

which i got from here. After that, change your onChange event to this:

onChange={debounce(onParamChange, 250)}

I hope this works, i can't test it right now.

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