DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

State Management in React (A Beginner-Friendly Guide)

If you’re completely new to frontend development and React, the term “state management” can sound intimidating. Don’t worry - it’s actually a very simple idea once you strip away the jargon.

This article explains state management in React in plain language, assuming zero frontend background. If you’re coming from backend development or general programming, you’ll feel right at home by the end.


What Is “State” in Simple Terms?

State is just data that can change while your app is running.

Think of a React app as a living page, not a static website.

Examples of state in real life:

  • Is a light ON or OFF?
  • How many items are in a cart? → 3
  • Is a user logged in? → true / false
  • What did the user type in an input? → "John"

All of these values can change, and when they do, the screen should update.

That changing data is what React calls state.


So What Is “State Management”?

State management is how you store, update, and share state.

In normal human language:

“Where do I keep my data, and how do different parts of my app use it?”

That’s it. No magic.


Before React: How Things Worked in Plain HTML + JavaScript

Let’s look at how you’d do this without React:

<p id="count">0</p>
<button onclick="increase()">+</button>

<script>
  let count = 0;

  function increase() {
    count++;
    document.getElementById("count").innerText = count;
  }
</script>
Enter fullscreen mode Exit fullscreen mode

What’s happening here:

  • count is your state
  • You manually update the screen using getElementById

This works for tiny apps, but in large applications it becomes:

  • Hard to track changes
  • Easy to break things
  • Very repetitive

React’s Big Idea (This Is Important)

When state changes, React automatically updates the screen.

You do not manually touch the DOM.

Instead:

  1. You update the state
  2. React re-renders the UI for you

This single idea is why React exists.


Your First React State Example

import { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <>
      <p>{count}</p>
      <button onClick={() => setCount(count + 1)}>
        +
      </button>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

What’s Happening Here?

const [count, setCount] = useState(0);
Enter fullscreen mode Exit fullscreen mode

This means:

  • count → the current value of the state
  • setCount → a function used to update the state
  • 0 → the initial value

When setCount is called:

  • React updates count
  • React re-renders the component
  • The UI updates automatically

You never touch the HTML manually.


A Rule You Must Memorize

Never change state directly

count = count + 1
Enter fullscreen mode Exit fullscreen mode

Always use the setter

setCount(count + 1)
Enter fullscreen mode Exit fullscreen mode

This rule prevents bugs and keeps React in control.


What Is a Component?

A component is just a JavaScript function that returns UI.

function Greeting() {
  return <h1>Hello</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Each component:

  • Is independent
  • Can have its own state
  • Re-renders when its state changes

Local State (The Easy Level)

Local state is state that belongs to one component only.

Examples:

  • A button being open or closed
  • Input field values
  • Toggle switches
  • Modal visibility
const [isOpen, setIsOpen] = useState(false);
Enter fullscreen mode Exit fullscreen mode

Mental model:

“This data belongs only here.”


The First Problem You’ll Hit: Sharing State

Imagine this situation:

  • Component A logs the user in
  • Component B needs to know if the user is logged in

They are separate components, but they need the same data.

Now what?

This is where state management starts to matter.


Step 1: Lifting State Up (React’s Native Solution)

The simplest solution is to move the state to a parent component.

function App() {
  const [user, setUser] = useState(null);

  return (
    <>
      <Login setUser={setUser} />
      <Dashboard user={user} />
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

How this works:

  • The parent owns the state
  • Child components receive data or functions as props

✔️ Simple
❌ Becomes messy in large apps


Step 2: Global State (For Bigger Applications)

When many components need the same data:

  • Logged-in user
  • Theme (dark/light)
  • Cart items
  • Notifications

Passing props through many layers becomes painful.
This problem is called prop drilling.

To solve it, we use global state.


Common State Management Options in React

1️⃣ React Context (Built-In)

Good for:

  • Authentication
  • Theme
  • Language settings

Think of it as:

“Make data available everywhere without passing props.”

✔️ No extra library
❌ Can get messy or slow if overused


2️⃣ Redux / Redux Toolkit

Good for:

  • Large applications
  • Complex data flows

Mental model:

“One central store for all application data.”

✔️ Predictable and structured
❌ More concepts to learn


3️⃣ Zustand / Jotai (Modern & Lightweight)

Good for:

  • Cleaner global state
  • Less boilerplate
  • Faster setup

These are very popular in modern React apps.


The Most Important Mental Model

Think of React state like backend data:

React Concept Backend Equivalent
Local state Local variable
Shared state Function arguments
Global state Database / cache

Once you see it this way, everything clicks.


Recommended Learning Path (Don’t Skip Steps)

Especially if you’re backend-minded:

  1. Learn useState
  2. Learn passing props
  3. Learn lifting state up
  4. Learn useContext
  5. Then learn Redux or Zustand

Jumping straight to Redux is a very common beginner mistake.


One-Sentence Summary

State management in React is simply about where your changing data lives and how different parts of your app access and update it.

Top comments (0)