Tracking the status of an application is how a single page application (SAP) logs changes and reports them to the user. Unlike a template framework, view, controller (MVC), SAP does not redirect the user and therefore needs a way to manage upcoming changes on the client side. To review MVC frameworks, refer to this guide.
In React, components themselves can handle their own state and re-spawn accordingly. This is done through life cycle methods, which are discussed in depth here. For the purposes of this article, we’ll take a look at how to change states using setState.
This guide assumes some basic familiarity with React. To review or learn React basics, this article lists some of the best React learning resources.
What is React setState?
The React setState method is like changing the state of a component with stability. There are a few specific rules for changing status in React.
- setState cannot be called in a class component. The components of the class call the constructor () method
and set to an initial state. Then we can change the state further down by calling setState.
There are two different types of components in React. It is the functional component and the class component. For our purposes, we are only talking to the components of the class. For a more in-depth look at this article on the differences between functional and class components. - By only calling setState in a class component, we can directly refer to the component itself using the this keyword. Calling
this.setState ()
is best practice and ensures your code does not break. - Do not modify the status directly. Direct state change will not be recorded on React to Spawning. The whole point of state change is to trigger a new interpretation of that component to reflect the changes made to the user. ol>