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…