Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda