Skip to content

Instantly share code, notes, and snippets.

@kishoreandra
Last active August 12, 2022 10:02
Show Gist options
  • Save kishoreandra/a72ccd6c56b6ab9e80bc4afeb61bae06 to your computer and use it in GitHub Desktop.
Save kishoreandra/a72ccd6c56b6ab9e80bc4afeb61bae06 to your computer and use it in GitHub Desktop.
state setter and updater in react useState - Leebo explained
To be specific, currentValue is the value the state had last time the component rendered. It isn't under any obligation to keep "up to date," because you want to start a new render when that happens. It's sort of like taking a blurry photograph vs a crisp clear one. Trying to render your component with a "always up to date" state would cause a blurry photo, because the state would be different by the time you got to the bottom of the render than it was at the top. Instead, the entire render shows you the same state from top to bottom, just like a clear photo
This is essentially one of the problems with class components vs functional components; the class architecture encouraged devs to "take blurry photos" all the time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment