What will happen if I use setState() function in constructor of a Class in ReactJS or React Native?

What setState essentially does is to run a bunch of logic you probably don’t need in the constructor.

When you go state = {foo : "bar"} you simply assign something to the javascript object state, like you would any other object. (That’s all state is by the way, just a regular object local to every component).

When you use setState(), then apart from assigning to the object state react also rerenders the component and all its children. Which you don’t need in the constructor, since the component hasn’t been rendered anyway.

Leave a Comment