Skip to main content

Command Palette

Search for a command to run...

React Hook: useState

Updated
5 min readView as Markdown
React Hook: useState
O
Tech Enthusiast/Technical Writer

Software interfaces look simple on the surface. You click a button, and a number changes. You type in a field, and text appears. But behind that smooth interaction is a small but powerful idea: state. In React, the most common tool for managing state in functional components is called useState.

Think of useState as React’s way of giving a component memory. Without it, a component would behave like a calculator that forgets everything the moment it finishes computing. We don't want that right?

With the state, the component remembers values between renders and can update the UI when those values change.

Let’s unpack this carefully.

What is useState?

useState is a React Hook that allows functional components to store and update data. That stored data is called state.

In simple terms:

  • State = data that can change over time

  • When the state changes, React re-renders the component to reflect the new value.

useState returns two things:

  1. The current state value

  2. A function used to update that value

Basic Syntax

import { useState } from "react";

function Example() {
  const [value, setValue] = useState(initialValue);

  return <div>{value}</div>;
}

Let’s decode that line:

const [value, setValue] = useState(initialValue);
  • value → the current state value

  • setValue → the function used to update the state

  • initialValue → the starting value of the state

Whenever setValue() runs, React updates the state and re-renders the component.

Understanding State with a Simple Counter

A counter is one of the clearest ways to understand state.

Imagine a page with:

  • A number displayed

  • A button that increases the number

Without state, the number would never change.

With useState, React remembers the value and updates the UI.

Counter Example

import { useState } from "react";

function Counter() {

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

  const increaseCount = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <h2>Counter: {count}</h2>
      <button onClick={increaseCount}>Increase</button>
    </div>
  );
}

export default Counter;

Let’s examine what is happening.

Step 1 — Setting the Initial State

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

Here we create a state variable called count.

  • count starts with the value 0

  • setCount is the function used to update count

So at the beginning:

count = 0

Step 2 — Updating the State

const increaseCount = () => {
  setCount(count + 1);
};

When the button is clicked:

  1. increaseCount() runs

  2. setCount() updates the value

  3. React re-renders the component

  4. The UI displays the new number

Example progression:

Initial render → Counter: 0
Click button → Counter: 1
Click again → Counter: 2
Click again → Counter: 3

Each click changes the state, and React automatically updates the UI.

Value and Update Function

useState always returns a pair.

const [value, setValue] = useState(initialValue);

Think of it like a small machine:

Part Meaning
value the current stored data
setValue The function that updates the data

Example:

const [name, setName] = useState("Segun");
  • name → current value "Segun"

  • setName("David") → updates the state

After update:

name = "David"

React then re-renders the component so the UI reflects the new value.

Why React Applications Are Stateful

A stateful application is one where the interface changes depending on stored data.

Without a state, applications would be static.

Examples of UI state:

Feature State Example
Counter number increases
Form input text typed by user
Dark mode theme toggled
Cart items added/removed
Login status authenticated or not

All of these rely on state changing over time.

In React, that state often lives inside components through hooks like useState.

The moment the state changes:

State change → React re-renders → UI updates

This reactive cycle is the core idea behind React.

Why React Uses useState

Do you know that before React Hooks existed, developers had to use class components to manage state? Hooks like useState made state management much simpler.

Benefits:

  • Works inside functional components

  • Less boilerplate

  • Easier to read

  • Easier to reuse logic

Mental Model for Beginners

A helpful way to imagine useState is this:

Your component is like a whiteboard.

  • useState writes a value on the board

  • setState erases and writes a new value

  • React then redraws the UI using the updated board

A Slightly Improved Counter

Here is a version with increase and decrease buttons.

import { useState } from "react";

function Counter() {

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

  return (
    <div>
      <h2>Counter: {count}</h2>

      <button onClick={() => setCount(count + 1)}>
        Increase
      </button>

      <button onClick={() => setCount(count - 1)}>
        Decrease
      </button>
    </div>
  );
}

export default Counter;

Now the state can move in both directions.

Final Thoughts

useState is one of the most important tools in React because it introduces statefulness into components.

It allows components to:

  • remember values

  • Update those values

  • re-render automatically when changes occur

In essence:

useState = memory + UI updates

Once this idea clicks, the rest of React forms, dynamic interfaces, dashboards, modals, and authentication states start making a lot more sense.

The next conceptual leap most developers encounter after useState is learning how multiple components share and coordinate state, which leads into ideas like lifting state up, context, and state management patterns. That’s where React applications begin to scale from simple counters into full interactive systems.