What is a debounce function and how do I use it in React?

The other day I was tasked with doing a code test for a front end assignment. The test was pretty straight forward — build a React application with a search feature for finding characters, using an API (fictional characters involved in intergalactic warfare, you might have heard about it). The application would, among other things, be judged on performance — specifically the avoidance of unnecessary API calls.

I told a friend of mine about the test and they immediately preached “use a debouncer!”. I knew it had something to with optimizing search bars, and after some research, I realized what a brilliant piece of algorithm it is.

What does it do?

In a nutshell, a debounce function makes sure a function isn’t called until the number of calls has “calmed down”. A few examples:

  • Don’t run an onclick handler until the user has stopped mashing a button
  • Waiting for a mousemove to stop
  • Wait until the user has “paused” typing into search bar

The latter is my scenario. I don’t want an API call to be made for every letter typed into the searchbar.

How does it work?

Let’s take a look at an ES6 version of the algorithm and break it down line by line.

A neat function (at least without the comments). Now let’s implement it in my React search bar component.

How to implement it in React

I added my debounce function in my file with utility functions, and exported it from there. I then imported it into my search bar component into a function variable with my API call and 300ms as arguments. The function is called for every onChange in the search input. This should make the API call only fire when the user stopped typing for 300ms. However, it did not work as expected.

const debouncedSearch = debounceEvent(val => {
}, 300);
function handleOnChange(event) {

Logging every debounce function call and every actual API call, I found that my search bar still fired an API call for every keystroke, after 300ms each. It turns out, a new instance of debouncedSearch is created for each re-render. It hit me that that’s how React works and the reason we need hooks.

Hooks to the rescue

This is the perfect opportunity to use the useCallback hook, because referential equality matters. Definition of useCallback from React Docs:

Pass an inline callback and an array of dependencies. useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders.

In this case, we want to prevent unnecessary API calls, and we only want one version of our callback so we pass in an empty dependency array. Our final debouncedSearch looks just slightly different from before:

const debouncedSearch = useCallback(
debounceEvent(val => {
}, 300),

Thanks to the useCallback hook, my search bar works as expected. To demonstrate, I’ve console logged the user input as well as the creating, clearing and firing of timers along with their ids. (The first one is undefined due to the order of the code expressions. This is fine.)

Quickly typing “dar” and firing an API call once stopped typing

So there you have it, a use case of debouncing, and how to implement it with React (using hooks).




Mpya Digital is an IT consultancy in Stockholm, Sweden. It is an organization made by — and continuously improved by — the people in it.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create an API in Node, Express & MongoDB part-2

A Canvas animation with React

[Mini Project]: LOTR in Vue.js 🧙🏽‍♂️Axios + ElementUI + Router 👾

Is Djitsu like X?

How I built my first chrome extension: a simple tool that makes YouTube better

Implement Binary Search of an Array in JavaScript

tiptop customer care number/8584892730//8584892738/tiptop customer care…

What’s side effects ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mpya Digital

Mpya Digital

Mpya Digital is an IT consultancy in Stockholm, Sweden. It is an organization made by — and continuously improved by — the people in it.

More from Medium

React: useState vs useReducer

Advanced React Hooks Handbook Part-4

Portals in React