← All posts

Understanding State in React: A Friendly Guide for Students

Learn how React’s state keeps your UI in sync—simple, clear, and student‑friendly. Master state updates, avoid pitfalls, and build interactive components with confidence.

The simple version

Imagine you’re building a small web app that shows a counter and lets users click a button to increase it. The number that appears on the screen is the state of that part of the app. In React, state is simply the data that can change over time and that influences what the component (the piece of UI) shows. Think of it like a notebook that remembers the current number, and whenever you write a new number, the notebook updates and the app refreshes to display the new value.

In this article we’ll keep it light and practical—no deep dives into the React engine. We’ll cover what state is, how it works, how to use it, and a few common pitfalls to avoid. By the end, you should feel comfortable adding simple state to your own React components.


What Is State?

State is the “memory” of a component. Every component can have its own set of data that starts with an initial value and can change in response to user actions or other events. In React, state lives inside a component and is kept separate from the component’s props (data passed in from a parent).

Analogy: Think of a component like a tiny, self‑contained room. The state is the furniture inside that room—chairs, tables, a lamp. If you move a chair or switch on the lamp, the room looks different. React updates the UI to match the new arrangement.

A component’s state is always an object (in class components) or a simple value (in functional components with hooks). The key idea is that when state changes, React automatically re‑renders the component to reflect the new data.


How State Works

When you create a component, you give it an initial state. React keeps that state in memory and gives you a special function to update it. Updating the state does two things:

  1. Changes the data – the new value replaces the old one.
  2. Triggers a re‑render – React calls the component again so that the UI shows the latest state.

Because React manages the re‑render, you never manually touch the DOM. You simply change state, and React does the rest.

Class component example

class Counter extends React.Component {
  constructor(props) {
    super(props);
    // initial state
    this.state = { count: 0 };
  }

  increment = () => {
    // update state
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.increment}>Add one</button>
      </div>
    );
  }
}

Functional component example (using hooks)

import { useState } from 'react';

function Counter() {
  // initial state: count = 0
  const [count, setCount] = useState(0);

  const increment = () => setCount(count + 1);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Add one</button>
    </div>
  );
}

Both snippets do the same thing: they store a number (count) and update it when the button is pressed. The UI automatically updates because React knows that count is part of the component’s state.


Using State in Components

1. Start with a clear initial value

Decide what the component should show when it first loads. In the counter example, the initial value is 0. In a todo list, the initial state might be an empty array.

2. Update state with the provided function

  • Class components: use this.setState() and pass an object with the new values.
  • Functional components: use the updater function returned by useState(), e.g., setCount(newValue).

3. Don’t mutate state directly

Treat state as read‑only. Instead of doing this.state.count++ or count.push(item), always create a new value and pass it to the updater. This helps React detect changes and keeps your UI predictable.

4. Keep state local when possible

If a piece of data only affects one component, store it there. If many components need the same data, consider lifting state up to a common ancestor or using a context or state‑management library.

5. Use state for UI decisions

State is great for things like:

  • Whether a modal is open or closed
  • Which tab is active
  • The current value of an input field
  • The loading status of data from an API

Anything that can change during the user’s interaction belongs in state.


Common Mistakes and Tips

Mistake What Happens Fix
Mutating state directly React may not notice the change and the UI stays the same Create a new value instead of mutating
Using this.state inside setState without a callback Race conditions when multiple updates happen quickly Use the functional form: this.setState(prev => ({ count: prev.count + 1 }))
Storing large objects in state Makes re‑renders slower Keep state minimal; store only what’s needed
Not initializing state undefined can cause errors in JSX Set a sensible default in the constructor or useState

A quick sanity check: If your component renders a value that isn’t updating, double‑check that you’re calling the state updater and not accidentally re‑assigning the state variable.


What to try next

  1. Build a simple toggle – Create a component with a button that flips between “ON” and “OFF” using a boolean state value.
  2. Create a list of items – Start with an empty array in state, add an “Add item” button that pushes a new string onto the array, and display the list below.
  3. Experiment with async data – Write a component that fetches a random joke from an API when a button is pressed, store the joke in state, and display it.

These exercises will give you hands‑on experience with initializing, updating, and rendering state in React. Happy coding!

FAQ

What is the difference between state and props in React?

State is internal mutable data that a component controls, while props are read‑only values passed from a parent component.

How do I initialize state in a functional component?

Use the useState hook: const [value, setValue] = useState(initialValue);

Why should I avoid mutating state directly?

Direct mutation bypasses React’s change detection, so the UI may not update and bugs can arise.

When should I lift state up?

Lift state when multiple sibling components need to share or synchronize data.