Skip to content

Instantly share code, notes, and snippets.

@npverni
Created May 22, 2020 14:22
Show Gist options
  • Save npverni/f1559fc81e3b80021ba2018b322978c5 to your computer and use it in GitHub Desktop.
Save npverni/f1559fc81e3b80021ba2018b322978c5 to your computer and use it in GitHub Desktop.
useDebounce hook with useState and useEffect
// import useDebounce
import { useDebounce } from './hooks';
// store searchTerm in state
const [searchTerm, setSearchTerm] = useState('search');
// setup a debounced version of searchTerm
const debouncedSearchTerm = useDebounce(pricePointSearchTerm, 500);
// Only hit the API when the debounced version changes
useEffect(() => pricePointSearch(), [debouncedSearchTerm]);
return (
<UIFormTextInput
value={searchTerm}
onChange={(_, value) => setSearchTerm(value)}
/>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment