Skip to main content

Command Palette

Search for a command to run...

Understanding useState in React JS β€” Beginner-Friendly Guide

Updated
β€’2 min read
Understanding useState in React JS β€” Beginner-Friendly Guide
S

I’m an Incoming SDE Intern (.NET) focused on improving my skills in C#, JavaScript, and SQL. I share what I learn through simple explanations to help other beginners grow in their coding journey πŸš€.

State is one of the most important concepts in React. If you understand useState, you understand 50% of React.
In this blog, you’ll learn what state is, why we use useState, and how it works with real examples.


βœ… What is State in React?

State means the current data of your component β€” data that can change over time.

Examples of state:

  • Counter value

  • Input text

  • Theme (dark/light mode)

  • Modal open/close status

When state changes β†’ React re-renders automatically.


βœ… Why do we use useState?

useState lets React remember values between re-renders.

It can store:

  • Numbers

  • Strings

  • Booleans

  • Objects

  • Arrays

Whenever you update the state, React updates the UI.


πŸ”΅ How useState Works

useState returns an array with two values:

const [stateValue, setStateValue] = useState(initialValue);

βœ” stateValue β†’ current value

βœ” setStateValue β†’ function to update the value


πŸ“Œ Example 1 β€” Counter

import { useState } from "react";

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

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

export default Counter;

πŸ” Explanation

  • Initial value is 0

  • Button click updates the state

  • React re-renders the UI


πŸ“Œ Example 2 β€” Input Handling

function InputExample() {
  const [text, setText] = useState("");

  return (
    <div>
      <input 
        type="text"
        value={text}
        onChange={(e) => setText(e.target.value)}
      />

      <p>You typed: {text}</p>
    </div>
  );
}

βœ” Input changes update state instantly


πŸ“˜ Bonus β€” useState with Objects

const [user, setUser] = useState({ name: "", age: "" });

function updateName(e) {
  setUser({ ...user, name: e.target.value });
}

πŸ”‘ Always copy the previous object with ... (spread operator)


⭐ Common Mistakes Beginners Make

❌ Updating state directly

count = count + 1; // Wrong

βœ” Correct way

setCount(prev => prev + 1);

🧠 Best Practices for useState

βœ” Use one state per purpose
βœ” Never mutate objects/arrays directly
βœ” Use callback when depending on previous state
βœ” Keep your state minimal


🏁 Final Thoughts

useState is the base of React's reactivity. Once you understand it, you can easily build dynamic features like forms, counters, toggles, filters, and modals.

Your next hook to learn β†’ useEffect.

Note: Originally published on Medium.

More from this blog

React with Sandip

7 posts

React with Sandip is a beginner-friendly publication where I share React concepts in simple language, helping new developers learn from basics to advanced with examples and step-by-step guides.