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

150
Views
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 answers
Answer question

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 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!