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

151
Visualizações
Debounce function in react js functional component

I tried to implement the debounce functionality in the react functional component. But due to some reason, it is not working.

import React from "react";
import "./styles.css";

export default function App() {
  const [random, setRandom] = React.useState(Math.random());
  const changeKey = () => {
    setRandom(Math.random());
  };

  const debounce = (func, timeout = 1000) => {
    let timer;
    return (...args) => {
      clearTimeout(timer);
      timer = setTimeout(() => {
        func.apply(this, args);
      }, timeout);
    };
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <button
        onClick={() => {
          debounce(changeKey(), 2000);
        }}
      >
        <p>Click Me!</p>
      </button>
      {random}
    </div>
  );
}

here I have added the codesandbox link https://codesandbox.io/s/silent-silence-6i532c?file=/src/App.js:0-857

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

0

Consider implementing it with a hook. This one, useDebounce from react-use is very good. https://github.com/streamich/react-use/blob/master/docs/useDebounce.md

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