🧠 useState in React – The OG Hook That Started It All 🚀

Amandeep SinghAmandeep Singh
3 min read

React Hooks came in like a wrecking ball in v16.8, and leading the charge was our hero: useState.

It’s simple, powerful, and often misunderstood. Like a Bollywood hero in a 90s movie. 🎬💥


🪝 What is useState?

In one line:

useState lets you add state to your functional components.

Or in React’s own words: “Wanna make your component remember stuff? Use useState.”

🧠 Without state:

function Counter() {
  let count = 0;
  return <button onClick={() => count++}>Click me</button>;
}

😐 Nothing happens. Because React doesn’t remember count.

✅ With useState:

import { useState } from "react";

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

  return (
    <button onClick={() => setCount(count + 1)}>
      You clicked {count} times
    </button>
  );
}

🎉 Now React re-renders and remembers the new count every time you click!


⚙️ The Syntax

const [state, setState] = useState(initialValue);

📦 state: current value
🔧 setState: function to update the value
🎁 initialValue: the default (initial) state


🧪 Real Life Example – A Like Button ❤️

function LikeButton() {
  const [liked, setLiked] = useState(false);

  return (
    <button onClick={() => setLiked(!liked)}>
      {liked ? "💖 Liked" : "🤍 Like"}
    </button>
  );
}

🧠 Tip: Never update state directly like liked = true – always use setLiked.


🧠 State is Async (kinda)

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

  const handleClick = () => {
    setCount(count + 1);
    console.log(count); // 😱 Still old value!
  };

  return <button onClick={handleClick}>Click</button>;
}

Why? Because React batches updates and re-renders after the event handler.


✅ Solution: Use Functional Update When Needed

setCount(prevCount => prevCount + 1);

Why? Because sometimes you’re updating based on the old state.

🎯 Meme Moment:
When you try setCount(count + 1) twice in a row and it only increases by 1
🧠 “State updates are async, bro”


🧺 Example: Managing Multiple States

function ProfileForm() {
  const [name, setName] = useState("");
  const [age, setAge] = useState("");

  return (
    <>
      <input value={name} onChange={(e) => setName(e.target.value)} />
      <input value={age} onChange={(e) => setAge(e.target.value)} />
    </>
  );
}

Or use an object for grouped state:

const [profile, setProfile] = useState({ name: "", age: "" });

<input
  value={profile.name}
  onChange={(e) => setProfile({ ...profile, name: e.target.value })}
/>

📛 Warning: Always spread the previous state to avoid wiping out other fields.


🧹 Don’t Confuse with Variables

This doesn’t work:

let count = 0;
const handleClick = () => {
  count++;
};

React doesn’t track normal variables. Only state updates cause re-renders.

📸 Meme Moment:
You changed a variable but nothing updated
React: "Main kya karun, job chhod du?" 😆


🎓 Bonus: Lazy Initialization

const [expensiveState, setExpensiveState] = useState(() => {
  console.log("Only runs once!");
  return computeExpensiveStuff();
});

Useful if initialValue is heavy to compute – React will only call it once.


💡 Tips

✅ Use one state per concern
✅ Use functional update when relying on old state
❌ Don’t mutate state directly
❌ Don’t read updated state right after calling setState


🔚 Wrapping Up

useState is like Maggie noodles 🍜 — easy to make, but mess it up and it’s a disaster!

Use it wisely and you can make your UI interactive, dynamic, and... well, alive 💡


🙌 Final Meme

When you finally understand useState

“Maa, main React samajh gaya!”


💬 Over to You

What’s your favorite useState bug story? Drop a comment or meme below ⬇️
And if you enjoyed this, don’t forget to ❤️ the post and share it with your dev friends!


0
Subscribe to my newsletter

Read articles from Amandeep Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amandeep Singh
Amandeep Singh

At GetReplies we are building a SaaS product to craft personalized emails, messages to target 10% reply rate. Zolo fosters impactful contributions as an SDE-III, where frontend engineering expertise drives scalable web solutions. Proficiencies in ReactJS, Next.js, and Redux Toolkit enable the creation of user-centric, high-performance applications. Success is achieved through seamless collaboration and continuous improvement.