React setState does not immediately update the state

Marcel Mokos
Mar 27 · 2 min read

React hooks are now preferred for state management. Calling setState multiple times in one function can lead to unpredicted behavior read more.

Image for post
Image for post

React State

Think of setState() as a request to update the component. Reading state right after calling setState() a potential pitfall.

Image for post
Image for post

useState React hook

Returns a stateful value, and a function to update it. The function to update the state can be called with a new value or with an updater function argument.

const [value, setValue] = useState("");

updater argument

If you need to set the state based on the previous state. Updater argument is guaranteed to fire after the update has been applied.

The first argument is an updater function with the signature:

(state) => newState

Use of updater function for toggle

const [isVisible, setVisible] = useState(false);

How we can setState and get state right after calling setState?

State right after calling setState will have value before the update.

const [value, setValue] = useState("");

We have some options.

Calling updater just to get the latest value.

const [value, setValue] = useState("");

Custom hook for setState with async state selector.

const [value, setValue, getValue] = useSetState("");

Implementation of a custom hook with async state selector with tests in Typescript.

The useSetState custom hook or have a look at Github https://github.com/marcelmokos/react-set-state-get-state/tree/master/

👏Clap, 👂follow for more awesome 💟#Javascript and ⚛️#React content.

ableneo Technology

Application Development, Data Science, Experience Platforms

Marcel Mokos

Written by

I'm fanatic to next generation Javascript lambda, yield, async/await everything. I admire typescript and flow types. Javascript will ultimately rule the world.

ableneo Technology

Application Development, Data Science, Experience Platforms

Marcel Mokos

Written by

I'm fanatic to next generation Javascript lambda, yield, async/await everything. I admire typescript and flow types. Javascript will ultimately rule the world.

ableneo Technology

Application Development, Data Science, Experience Platforms

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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