prevState() is the same as the setState but the only difference between them is that if we want to change the state of a component based on the previous state of that component, we use this. setState() , which provides us the prevState . Let's check an example of a counter app.
What is the second argument that can optionally be passed to setState and what is its purpose ? The second argument that can optionally be passed to setState is a callback function which gets called immediately after the setState is completed and the components get re-rendered.
setState is done to 'set' the state of a value, even if its already set in the 'getInitialState' function. Similarly, The replaceState() method is for when you want to clear out the values already in state, and add new ones.
render() Calling setState() here makes it possible for a component to produce infinite loops. The render() function should be pure, meaning that it does not modify a component's state. It returns the same result each time it's invoked, and it does not directly interact with the browser.
Both signatures can be used, the only difference is that if you need to change your state based on the previous state you should use this.setState(function)
which will provide you a snapshot(prevState
) from the previous state. But if the change does not rely on any other previous value, then a shorter version is recommended this.setState({prop: newValue})
this.setState(prevState =>{
return{
...prevState,
counter : prevState.counter +1
}
})
this.setState({counter : 2})
class MyApp extends React.Component {
state = {
count: 0
};
Increment = () => {
this.setState(prevState => ({
count: prevState.count + 1
}));
this.setState(prevState => ({
count: prevState.count + 1
}));
this.setState(prevState => ({
count: prevState.count + 1
}));
this.setState(prevState => ({
count: prevState.count + 1
}));
};
IncrementWithoutPrevState = () => {
this.setState(() => ({
count: this.state.count + 1
}));
this.setState(() => ({
count: this.state.count + 1
}));
this.setState(() => ({
count: this.state.count + 1
}));
this.setState(() => ({
count: this.state.count + 1
}));
};
render() {
return (
<div>
<button onClick={this.IncrementWithoutPrevState}>
Increment 4 times without PrevState
</button>
<button onClick={this.Increment}>
Increment 4 times with PrevState
</button>
<h1>Count: {this.state.count}</h1>
</div>
);
}
}
I just made an example for you to give an idea what is meant by "React may batch multiple setState()..." and why we should use prevState in the above scenario.
First, try to guess what should the result of Count
when you click both buttons... If you think the count
will be incremented by 4 on click of both buttons then it's not right guess ;)
Why? because in IncrementWithoutPrevState
method since there are multiple setState
calls, so React batched all those calls and updates the state
only in the last call of setState
in this method, so at the time of last call to setState
in this method this.state.count
is not yet updated and its value is still the same that was before entering into IncrementWithoutPrevState
method so the resultant state will contain count
incremented by 1.
Now on the other hand side if we analyze the Increment
method:
Again there are multiple setState
calls and React batched them all that means the actual state will be updated in the last call of setState
but the prevState
will always contain the modified state
in the recent setState
call. As previousState.count
value has already been incremented 3 times till the last call of setState
so the resultant state
will contain the count value incremented by 4.
here iseveryone know just state:prevstate.counter+1.. we can also do this with state:this.state.counter+1. This is not the way i think to use prevstate. i have problem in array when i push into existing state it allow me but second time it prevent changes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With