React - State

About

State in React.

State is similar to props, but it is private, fully controlled by the component and triggers a rendering

If the answer to the below question is yes, it probably isn't state.

  • Is it passed in from a parent component?
  • Does it remain unchanged over time?
  • Can you compute it based on any other state or props in your component?

In React apps, whether a component is stateful or stateless is considered an implementation detail of the component that may change over time. You can use stateless components inside stateful components, and vice versa.

React

Local

State in React refers to local state mainly. It is like a component’s personal data storage. State is useful for handling data that changes over time or that comes from user interaction. State gives components memory.

React does not persist the data between page load. You should persist your state locally or use a state library.

Global

Shared

Props

props are component arguments (passed in the form of object properties) that are passed from parent to child component. They are not really state but hold values.

Library

React does not persist the data between page load because it's mainly coupled with a state management library.

State management library aims to implement logging, hot reloading, time travel, universal apps, record and replay.

A state management library will trace every mutation to the action that caused it. You can record user sessions and reproduce them just by replaying every action.

Redux

Redux evolves the ideas of Flux, but avoids its complexity by taking cues from Elm.

  • The whole state of the app is stored in an object tree inside a single store.
  • The only way to change the state tree is to emit an action, an object describing what happened.
  • To specify how the actions transform the state tree, you write pure reducers.

Others

  • MobX,
  • Flux

Documentation / Reference


Powered by ComboStrap