---
title: "Discussion on \"ReactJS useState Hook - lazy initialization and previous state\" | Hashnode"
description: "Discussion on \"ReactJS useState Hook - lazy initialization and previous state\". ReactJS useState hook is a great way to manage the state of the functional components. Let's learn a couple of interesting usages about it."
url: "https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [ReactJS useState Hook - lazy initialization and previous state](https://blog.greenroots.info/react-hook-usestate-lazy-initialization-previous-state?utm_source=hashnode&utm_medium=feed)

ReactJS useState hook is a great way to manage the state of the functional components. Let's learn a couple of interesting usages about it.

Feb 17, 2022 · 5 min read · blog.greenroots.info  
Updated May 3, 2023  
Read the article

-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#javascript](https://hashnode.com/tag/javascript)
-   [#codenewbies](https://hashnode.com/tag/codenewbies)
-   [#beginners](https://hashnode.com/tag/beginners)

## Written by

[Tapas Adhikary](https://hashnode.com/@atapas)

@atapas · Bangalore

  
[View profile](https://hashnode.com/@atapas)

Educator @tapaScript | Founder CreoWis & ReactPlay - Writer - YouTuber - Open Source

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human

## Responses 5

Markdown supported · ⌘↵ to post

[Um Zaharradeen](https://hashnode.com/@deeni)

[Feb 20, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/621270079ae0e67b657b898c)

great article keep this comming

[Tapas Adhikary](https://hashnode.com/@atapas)

[Feb 20, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/621270079ae0e67b657b898c#reply-621284f2e2db563535e3a325)

Thank you 😊

[Andrew Baisden](https://hashnode.com/@andrewbaisden)

[Feb 19, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/6210df8fc6ed3906e81c234b)

Good article [Tapas Adhikary](https://hashnode.com/@atapas).

[Tapas Adhikary](https://hashnode.com/@atapas)

[Feb 19, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/6210df8fc6ed3906e81c234b#reply-6210f33d30c8050670ed2f2f)

Thanks a lot 🙏

[Ankur Tyagi](https://hashnode.com/@TheAnkurTyagi)

[Feb 18, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/620f6933d589905b41292039)

Great blog post. [Tapas Adhikary](https://hashnode.com/@atapas)

[Tapas Adhikary](https://hashnode.com/@atapas)

[Feb 18, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/620f6933d589905b41292039#reply-620f6b659270326b0ebc755e)

Thanks a lot [Ankur](https://hashnode.com/@TheAnkurTyagi)

[Engin Yılmaz](https://hashnode.com/@Veridelisi2)

[Feb 18, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/620f55099270326b0ebc7289)

I dont understand that difference

const incrBy3 = () => { setCounter((prev) => prev + 3); };

const incrBy3 = () => { setCounter(counter+ 3); };

[Tapas Adhikary](https://hashnode.com/@atapas)

[Feb 18, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/620f55099270326b0ebc7289#reply-620f6154d589905b41291f47)

In the above examples, the outcome is the same. But in the case of the first one, you are using the prev state value explicitly. This is handy in many cases where you use the previous state value to compute and set the current state value.

The `prev` is guaranteed to have the prev state value, whereas there could be cases that relying on just the `counter` may not get you the previous state value. It may happen when you update the state on an event(say click event) and the event occurs very fast.

The state update may not happen as expected because the onClick props function creates a closure on the `counter` value. So if the onClick triggers too fast, the counter value may not get updated constantly as expected. Using the `prev` state will make sure it does.

[Engin Yılmaz](https://hashnode.com/@Veridelisi2)

[Feb 18, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/620f55099270326b0ebc7289#reply-620f80e6d589905b412922f0)

Thank you for detailed explanation. I hope you write more in react issues. More compact more fast ![Tapas Adhikary](https://hashnode.com/@atapas)

[Tapas Adhikary](https://hashnode.com/@atapas)

[Feb 18, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/620f55099270326b0ebc7289#reply-620fa0678f26523301cb4e10)

[Engin Yılmaz](https://hashnode.com/@Veridelisi2) Thanks a lot. Will do. For the moment focusing on the React series on YouTube 🤩

[Paul Chapman](https://hashnode.com/@paul346)

[Feb 17, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/620e628425673f2740eec3f8)

Great article [Tapas Adhikary](https://hashnode.com/@atapas)

[Tapas Adhikary](https://hashnode.com/@atapas)

[Feb 17, 2022](https://hashnode.com/posts/react-hook-usestate-lazy-initialization-previous-state/620e4efe25673f2740eec1b9/comment/620e628425673f2740eec3f8#reply-620e65a9d589905b41290d02)

Thanks a lot, [Paul](https://hashnode.com/@paul346)
