DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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.

Top comments (0)

DEV has this feature:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›