C1CTech

ReactJS|State

In the previous article on ReactJS|Props  we have talked about What are props? Now What if we want to manage some data inside the component that may change over time. To achieve this React comes with State. Now, what is state? In this article, we will talk about react state in greater detail.

What is State?

Difference between Props and State

We have already learned about Props and we got to know that both are plain JavaScript objects and both hold information that influences the output of render but they are different in the following way:

Now we have learned the basics of State and are able to differentiate it from Props. Now all that is left is to know about how to work with state before implementing one for ourselves.

State is usually defined in the constructor of the component’s class with the default initial value. To define the state of any Class we can use the code format below :

state_in_constructor_new

Let’s take a look at simple example demonstrating the working of state in react :

 

In the above example when we click on the increment button the incrementCount method will call and inside it, we are calling setState method. This method accepts an object which is nothing but the new state of the component. In the new state, we are incrementing the count property value by one.

Given below is the generated output when we execute the above code :

 

Do’s and Don’t with state and setState

Never modify the state directly.

Never modify the state directly instead make use of setState because when we modify the state directly react will not re-render the component. setState, on the other hand, will let react know it has to re-render the component.  For example, if we update the state of any component like the following the webpage will not re-render itself.

Given below is the generated output when we execute the above code :

In the above output, you can see when we click on the increment button the value of count is not incremented in the UI but in the console, it has changed from 0 to 1.If we do few more clicks you can see its same 0 in the UI and increments in the console. What this means is that UI is not re-rendering whenever the state is changing and this is the main reason we should never modify the state directly.

React provides its own method setState(). setState() method takes a single parameter and expects an object which should contain the set of values to be updated. Once the update is done the method implicitly calls the render() method to repaint the page. Hence the correct method of updating the value of a state will be similar to the code below.

Given below is the generated output after executing the above code:

In the above output when we click on increment button the value of count in the UI changes from 0 to 1 but in the console, the value is 0. So everytime when we click on button console value is one less than the rendered value and this is because calls to setState are asynchronous so what is going on is console.log is calling before the state is actually set.

Place the code in the callback method of setState which you want to execute after the state has been updated.

Calls to setState are asynchronous. So whenever you need to execute some code after the state has been changed then do not place that code right after the setState method. Instead, place that code within the componentDidUpdate or the callback method that is passed as the second argument to the setState method, either of which is guaranteed to fire after the update has been applied.

Given below is the generated output after executing the above code:

In the above output when we click on increment button the value of count in the UI changes from 0 to 1 and in the console we have a value of 0 and callback value of one. Zero is from the synchronous console.log statement and 1 is from callback function console.log statement.

Let’s take a look at the next scenario when we will try to use the current state to calculate the new state. The above code is working as expected and the increment is working fine. This is because the current scenario is pretty simple. Let’s make the scenario slightly complicated :

So now the count should increment from 0 to 3 when we click on the increment button.

In the above output when we click on increment button the value changes to 1 instead of changing to 3 and in the console zero is logged three times and even the callback value of one is logged three times. This behavior is because react may group multiple setState calls into a single update for better performance.So what happens in our scenario is that all the three setState calls are done in one single go and the updated value does not carry over between the different calls.

When you have to update state based on the previous state value, pass in a function as an argument instead of a regular object.

To fix it, use a second form of setState that accepts a function rather than an object. That function will receive the previous state as the first argument, and the props object as the second argument. So whenever you want to update the state based on the previous state we need to pass the function as the argument to setState method instead of passing in an object.

Now we are going to use the previous state instead of the current state.

Given below is the generated output after executing the above code:

In the above output when we click on the button the value of count displayed as three. When we click again the count will change to six in the UI. Now we are able to correctly render the UI based on the state.

The second parameter passed to the updater function is the props object so if your new state depends on props object as well, you can go with the function parameter approach and make use of props.

I hope this article will help you in understanding the basics of state, how state is different from props and also the do’s and don’t while working with state and setState.Thank you.

 

Exit mobile version