DEV Community

Mohammed Nadeem Shareef
Mohammed Nadeem Shareef

Posted on

React useState hook is asynchronous!

Hello Developers πŸ‘‹

I would like to share something I recently got to know, so the background is, in my project I was using useState value right after updating it and I was getting previous value(not updated value) and to my surprise I found out that useState hook is asynchronous

what it is?

Basically, the thing is you don't get update value right after updating state.

What is the work around/solution?

We can use the useEffect hook and add our state in the dependence array, and we will always get the updated value.

Show me the code 🀩🀩🀩

import { useState } from "react";

export default function CountWithoutEffect() {
    const [count, setCount] = useState(0);
    const [doubleCount, setDoubleCount] = useState(count * 2);
    const handleCount = () => {
        setCount(count + 1);
        setDoubleCount(count * 2); // This will not use the latest value of count
    };
    return (
        <div className="App">
            <div>
                <h2>Count Without useEffect</h2>
                <h3>Count: {count}</h3>
                <h3>Count * 2: {doubleCount}</h3>
                <button onClick={handleCount}>Count++</button>
            </div>
        </div>
    );
}

Enter fullscreen mode Exit fullscreen mode
  • Here we have very simple and stright forward component.
  • On button click we are updating two states and one state is dependent on other state.
  • The doubleCount will be one step behind count.
  • Check out the Live Demo

Solving this issue 🧐🧐🧐

This can be easily solve with useEffect hook, let's see the code


import { useState, useEffect } from "react";

export default function CountWithEffect() {
    const [count, setCount] = useState(0);
    const [doubleCount, setDoubleCount] = useState(count * 2);
    const handleCount = () => {
        setCount(count + 1);
    };

    useEffect(() => {
        setDoubleCount(count * 2); // This will always use latest value of count
    }, [count]);

    return (
        <div>
            <h2>Count with useEffect</h2>
            <h3>Count: {count}</h3>
            <h3>Count * 2: {doubleCount}</h3>
            <button onClick={handleCount}>Count++</button>
        </div>
    );
}

Enter fullscreen mode Exit fullscreen mode
  • Here, when ever count changes we are updating doubleCount
  • Check out the live Demo

Closing here πŸ‘‹πŸ‘‹πŸ‘‹

This is Shareef.
Live demo
GitHub repo of this blog
Stackover flow link for more info
My Portfolio
Twitter ShareefBhai99
Linkedin
My other Blogs

Latest comments (26)

Collapse
 
camillo_targas_24cb74e289 profile image
Camillo Targas

Very good, thank you!

Collapse
 
mahikaushi65741 profile image
mahi kaushik

"Great article! Understanding the asynchronous behavior of the useState hook in React is essential for writing efficient and effective code. While it can be easy to assume that the useState hook works synchronously like other functions, it's important to remember that React's state updates are batched and processed asynchronously for performance reasons.

The key takeaway from your article is that relying on the current value of state immediately after calling useState may not always return the most up-to-date value. Instead, you should use the callback function syntax to update state based on its previous value.

For those interested in learning more about React's useState hook, I recommend checking out this React usestate blog. Thanks for sharing your insights!"

Collapse
 
froxx93 profile image
Froxx93

In most cases you don't even need to make doubleCount a state.
You can simply say const doubleCount = count * 2; and it'll just calculate it new when count updates. Sometimes, when you have a more complex setup, useEffect might come in handy though.

Collapse
 
manpat profile image
Manish

Though in the above example it may not seem to make sense, but maybe we are rendering a component based on the doubleCount(or any state which is dependent on another), then just the variable doubleCount doesn't help in rendering the component as it changes

Collapse
 
shareef profile image
Mohammed Nadeem Shareef

Thanks for the info man... 😊

Collapse
 
abdulazeem4 profile image
Azeem • Edited

Shareef Bacha

Collapse
 
abdullah565 profile image
abdullah565

Thanks for this article.
But I wanna add here useState is not async. It is actually sync.

why it doesn't update immediately because of the closure.
more on this here: youtube.com/watch?v=RAJD4KpX8LA

thanks :)

PS: i'm still learning it how it does work and how react render works under the hood.

Collapse
 
shareef profile image
Mohammed Nadeem Shareef

Thanks for the input.

Collapse
 
eliya profile image
Eliya • Edited

Another (bit-tricky) way to solve this problem, is by setting the 'count' state with a callback, which returns an IIFE by itself, that will take the result of 'count' incremental as argument, sets doubleCount, and returns back the argument, which is the finally returned value for setCound.

Collapse
 
rishi369 profile image
Rishi
Collapse
 
gabrielfmpinheiro profile image
Gabriel Pinheiro

Can I use async/ await too?

Collapse
 
shareef profile image
Mohammed Nadeem Shareef

tbh
I don't know exactly may be you can or can't.
I would prefer not to use it.

Collapse
 
simonxcode profile image
Simon Xiong

Thanks for the writeup. Recently had to complete a project and ran into this issue. Now I at least know how to resolve it in future cases. πŸ‘

Collapse
 
shareef profile image
Mohammed Nadeem Shareef

πŸ˜„

Collapse
 
diegoo11 profile image
Diegoo11

Muchas gracias, fue muy util.
Thank you very much, it was very useful.

Collapse
 
mightymit profile image
Mithun Varghese

So recently I learnt this the hard way too. But what I realized is if you await setCount call, although it doesnot return a promise, the following code will execute after the count has incremented. Hope it helps :)

Collapse
 
shareef profile image
Mohammed Nadeem Shareef

Thanks for the info. I would love to read the hard way. It might help many of us.

Collapse
 
lucilag profile image
Lucila Gaudio

I came to the same solution, I I think this is why: developer.mozilla.org/en-US/docs/W...