Created
May 22, 2020 14:22
-
-
Save npverni/f1559fc81e3b80021ba2018b322978c5 to your computer and use it in GitHub Desktop.
useDebounce hook with useState and useEffect
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
// 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