Last active
September 12, 2020 05:23
-
-
Save dev-sankhadip/e4850484ae47151f43e0bb8fde95619a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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