DEV Community

Diomedes Lajara
Diomedes Lajara

Posted on

State Rules!

Rules of state

  • Do not confuse props with state
  • ‘State’ is a JS object
  • Only usable with class based components
  • State has to be initialized when a component is created
  • Never update state directly.
  • State can only be updated with setState.
  • Updating State causes the component to re-render.

State object is defined inside the constructor function.
Use states (this.state) to manage dynamic data.

Constructor note
The base class of React.Component has its own constructor and by defining a new constructor, it gets overridden. But to make sure that the “parent” constructor gets called we use super(props). Super with props is a reference to the parent constructor inside React.Component function.

Discussion (0)