DEV Community

Diomedes Lajara
Diomedes Lajara

Posted on

1 1

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.

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay