Accepted answer

state and instance properties serve different purposes. while calling setstate with empty arguments will cause a render and will reflect the updated instance properties, state can be used for many more features like comparing prevstate and currentstate in shouldcomponentupdate to decide whether you want to render or not, or in lifecycle method like componentdidupdate where you can take an action based on state change.

state is a special instance property used by react to serve special purposes. also in setstate, state updates are batched for performance reasons and state updates happen asynchronously unlike class variable updates which happen synchronously. a class variable won't have these features.

also when you supply a class variable as prop to the component, a change in this class variable can't be differentiated in the lifecycle methods of the child component unless you are creating a new instance of the variable yourself. react does it with state property for you already.


both have different purpose. rule of thumb is:

  • use state to store data if it is involved in rendering or data flow (i.e. if its used directly or indirectly in render method)
  • use other instance fields to store data if value is not involved in rendering or data flow (to prevent rendering on change of data) e.g. to store a timer id that is not used in render method. see timerid example in official docs to understand this valid case.

if some value isn’t used for rendering or data flow (for example, a timer id), you don’t have to put it in the state. such values can be defined as fields on the component instance.


Related Query

More Query from same tag