How To Properly Change State in React

When learning React, you will be introduced to the tool React.Component.setState() and instructed to never update the state property directly. In any programming language, if you are provided an accessor method (a getter or a setter), it’s safe to assume that you shouldn’t access the variable directly. This holds true for setState() as well.

Why You Should Never Update State Directly

There are a few reasons to never update state directly:

  • setState() is asynchronous. If you set state…

--

--

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