Member-only story

Mastering State Management in React: App State vs Component State Explained

Rajesh Dhiman
4 min readOct 16, 2024

--

Imagine you’re playing a video game. You know how your character has health points, experience levels, and maybe even how many coins they’ve collected? All of that information needs to be stored somewhere so the game can keep track of it. In a React app, state is like your character’s stats in a game — it keeps track of important information while the app runs.

In this guide, we’ll break down state management in React, compare app state to component state, and show you how to manage both. Ready? Let’s jump in!

Photo by Rahul Mishra on Unsplash

What is State in React?

In React, state is like the memory of your app. It keeps track of things that can change, like what a user has typed into a form or which items are in their shopping cart. Without state, your app wouldn’t be able to remember anything between actions, like when a user moves from one page to another.

There are two main types of state:

  1. Component State: This is like your room in a house. It’s private and only affects the stuff inside your room (or in this case, the component).
  2. App State: This is like the living room that everyone shares. It affects the entire house (or app) and everyone can see it and interact with it.

--

--

Rajesh Dhiman
Rajesh Dhiman

Written by Rajesh Dhiman

Rajesh Dhiman is a full-stack developer, technical architect, and mentor with 12+ years of experience. https://www.rajeshdhiman.in/

No responses yet