Skip to content

Instantly share code, notes, and snippets.

@dev-sankhadip
Last active September 12, 2020 05:23
Show Gist options
  • Save dev-sankhadip/e4850484ae47151f43e0bb8fde95619a to your computer and use it in GitHub Desktop.
Save dev-sankhadip/e4850484ae47151f43e0bb8fde95619a to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
function App() {
const [name, setName] = useState("");
const debounce = (func, delay) => {
let debounceTimer;
return function () {
const context = this;
const args = arguments;
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func.apply(context, args), delay);
};
};
const update = debounce(function (e) {
console.log(e.target.value)
setName(e.target.value)
}, 1000);
return (
<div className="App">
<input type="text" onChange={(e)=>{ e.persist(); update(e); }} />
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment