DEV Community

Fatima Alam
Fatima Alam

Posted on

Debouncing in React

npm install lodash.debounce
import debounce from 'lodash.debounce'
Enter fullscreen mode Exit fullscreen mode
const doCityFilter = query => {
  if (!query) return setFilteredCities([])

  const debouncedFilter = debounce(() => {
    console.log('====>', query)
    setFilteredCities(citiesArray.filter(
      city => city.toLowerCase().includes(query.toLowerCase())
    ))
  }, 500)

  debouncedFilter()
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)