Skip to content

Instantly share code, notes, and snippets.

@codewithbernard
Created December 8, 2021 09:34
Show Gist options
  • Select an option

  • Save codewithbernard/a529147742fb9709aad2d257e537611a to your computer and use it in GitHub Desktop.

Select an option

Save codewithbernard/a529147742fb9709aad2d257e537611a to your computer and use it in GitHub Desktop.
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