Created
March 25, 2020 13:55
-
-
Save tacyarg/13b5f76d7c9a43f4d8a642750637e196 to your computer and use it in GitHub Desktop.
This file contains 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
function useDebounce(value, delay = 500) { | |
const [debouncedValue, setDebouncedValue] = useState(value); | |
useEffect(() => { | |
// Set debouncedValue to value (passed in) after the specified delay | |
const handler = setTimeout(() => { | |
setDebouncedValue(value); | |
}, delay); | |
return () => { | |
clearTimeout(handler); | |
}; | |
}, [value]); | |
return debouncedValue; | |
} | |
const Search = ({ onSearch = x => x, ...p }) => { | |
const [search, setSearch] = useState(""); | |
const debouncedSearchTerm = Utils.useDebounce(search, 500); | |
useEffect(() => { | |
onSearch(search); | |
}, [debouncedSearchTerm]); | |
return ( | |
<Input | |
{...p} | |
placeholder="Search..." | |
value={search} | |
onChange={e => setSearch(e.target.value)} | |
/> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment