Accepted answer

inside a react event handler (that is, a function called from a react-based onchange={...} property and the like), multiple calls to setstate are batched and the component is only re-rendered a single time. so, there's no difference between

handleclick: function() {
  this.setstate({fruit1: "apple"});
  this.setstate({fruit2: "mango"});


handleclick: function() {
  this.setstate({fruit1: "apple", fruit2: "mango"});

however, outside of the react event system, the two calls to setstate will not be merged unless you wrap them in a function passed to react.addons.batchedupdates. this is normally not something you have to worry about, but may become an issue if you start setting state in response to asynchronous events (e.g. timers, promises, callbacks, etc). for that reason, i would generally recommend the second form (with the two objects merged into one) over the first.


here is a detailed explanation of react states and setstate() method.

states (and props) are only two reasons why react re-renders/recalculate the dom. it means if we change state, we are telling react to change the related behaviour of our app.

state is a java-script object with key:value pair. now we may have many states (many key:value pairs) and let's say at certain point only one state is changing. in that case we may use this.setstate() method to change only that specific state.

state = { fruit1: 'mango', fruit2: 'apple' }

let's say we want to update fruit1: 'watermelon'.

here we can say:

this.setstate( {fruit1: 'watermelon'} ); 

here, we did not say anything about second state (fruit2), so react will merge changed state (fruit1) with old state (fruit2).

while, we can also say:

this.setstate( {fruit1: 'watermelon' ,fruit2:'apple'} ); 

but it is not necessary.

correct and re-commanded way of setting/changing state:

from react official docs:

think of setstate() as a request rather than an immediate command to update the component. for better perceived performance, react may delay it, and then update several components in a single pass. react does not guarantee that the state changes are applied immediately.

so here is a better way to do it: if we are updating the counter or calculating something:

this.setstate((prevstate,props) => {
 return {
           { counter: prevstate.counter+1; }


from react documentation:

never mutate this.state directly, as calling setstate() afterwards may replace the mutation you made. treat this.state as if it were immutable. setstate() does not immediately mutate this.state but creates a pending state transition. accessing this.state after calling this method can potentially return the existing value. there is no guarantee of synchronous operation of calls to setstate and calls may be batched for performance gains. setstate() will always trigger a re-render unless conditional rendering logic is implemented in shouldcomponentupdate(). if mutable objects are being used and the logic cannot be implemented in shouldcomponentupdate(), calling setstate() only when the new state differs from the previous state will avoid unnecessary re-renders.

so, use this.setstate({fruit1: “apple”, fruit2: “mango”});

for the second question you can look here and here

Related Query

More Query from same tag