Created
December 8, 2021 09:34
-
-
Save codewithbernard/a529147742fb9709aad2d257e537611a 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 { useState, useCallback } from "react"; | |
| import debounce from "lodash/debounce"; | |
| const Input = () => { | |
| const [inputValue, setInputValue] = useState(""); | |
| const sendQuery = (query) => { | |
| // Call API with query parameter here | |
| console.log(query); | |
| }; | |
| // Delay search by 600ms | |
| const delayedSearch = useCallback( | |
| debounce((q) => sendQuery(q), 600), | |
| [] | |
| ); | |
| const handleChange = (event) => { | |
| // Input will be changed immidiately | |
| setInputValue(event.target.value); | |
| // Search will only be called when user stops typing | |
| delayedSearch(event.target.value); | |
| }; | |
| return <input value={inputValue} onChange={handleChange} />; | |
| }; | |
| export default Input; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment