Making delayed network requests in React after state changes

Photo by Erik Mclean

Making delayed network requests in React after state changes

Posted on 13th September 2021

3 min read


Recently I switched the database used in my site for page views and reactions from PostgreSQL to PlanetScale.

With the change I also thought it would be a good chance to redesign the reactions section of the posts.

You can try it yourself. šŸ‘‡šŸ½

Implementing the button and animation with react-spring was relatively easy, but I had a choice of how I wanted to make the network request to save the result.

I could either save the result everytime a visitor clicks the button incrementing the value in the database by one, or wait for a certain time after the visitor finishes clicking and save that result.

Saving on every click would be the easiest but it would create problems such as race conditions, issues with the optimistic UI updates Iā€™m handling with React Query and rate limits with any hosting provider. So my best option was the second one.

How I implemented it

In my case I started with a state to store the reaction count.


I also had a value called count which is the acual reaction count in the database.


react is the function that is used to make the network request to update the value in the database.

The first thing I had to do was to create a side effect to make the network request after the state changes.


Next I used setTimeout to call react one second after reactionChanges. I also added an extra check to make sure react is not called if there is no difference between reactionCount and count.


Finally I had to handle the case where the visitor clicks the button multiple times all less than a second apart each other. In this case I had to use the useEffect cleanup function to remove the previous timeout in order for a new timeout.


So now when reactionCount changes, the timeout set for that particular value of reactionCount is cleared and a new timeout is set.

You can see the code in action just below. šŸ˜Š

If you enjoyed this post please leave a like or two below!!!