DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Debounce Example

import React from "react";
import { render } from "react-dom";

const debounce = (func, delay) => {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      func(...args);
    }, delay);
  };
};

const printData = (data) => {
  console.log("data", data);
};

// const getData = debounce((input) => printData(input), 5000);
// OR
const getData = debounce(printData, 5000);

const App = (props) => {
  return (
    <div>
      <h2>Debounce Example List</h2>
      <input type="text" onChange={(event) => getData(event.target.value)} />
    </div>
  );
};

render(<App />, document.getElementById("root"));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)