Skip to content

Instantly share code, notes, and snippets.

@tacyarg
Created March 25, 2020 13:55
Show Gist options
  • Save tacyarg/13b5f76d7c9a43f4d8a642750637e196 to your computer and use it in GitHub Desktop.
Save tacyarg/13b5f76d7c9a43f4d8a642750637e196 to your computer and use it in GitHub Desktop.
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