Let’s be honest—when you first hear “React Hooks”, it sounds a bit intimidating. Like something only senior developers casually throw around while sipping coffee
But here’s the truth: Hooks are not scary.
In fact, once you understand just one of them—useState—you unlock a huge part of React.
Think of this as building something step by step. Each piece adds meaning, and suddenly everything makes sense.
And that’s exactly why this is part of a series called “Let’s Master React Hooks Together”.
Because learning React (or honestly, anything in tech) becomes much easier when you don’t try to swallow everything at once. A series gives you structure. It keeps you consistent. It removes confusion about what to learn next. And most importantly, it builds discipline—one small concept at a time, instead of random jumping between topics.
When you follow a guided path like this, you’re not just learning React Hooks… you’re training your mind to learn efficiently, stay consistent, and actually finish what you start.
So instead of rushing, we go step by step—starting with useState and building real confidence along the way.
So… Why Do We Even Need useState?
useState exists to help your component remember things over time and update the UI when those things change.
Imagine you’re writing a number on a whiteboard:
function Counter() {
let count = 0;
return <h1>{count}</h1>;
}
function Counter() → this creates a component that React will run every time it renders
let count = 0; → a normal variable is created and always starts fresh at 0
return <h1>{count}</h1>; → React displays the current value on screen
You write 0. Someone says “increase it”… but every time the component runs again, it goes back to 0.
That’s exactly what happens here:
- Every render resets the value
- Nothing is remembered
So your app behaves like it has no memory at all.
➡️ We need a way to remember what happened before.
What Exactly is useState?
useState gives your component a memory slot that survives re-renders.
Let’s upgrade that whiteboard to something smarter:
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return <h1>{count}</h1>;
}
import { useState } from "react"; → brings the state feature into your component
function Counter() → React runs this function to render UI
const [count, setCount] = useState(0); →
-
countholds the current value -
setCountis used to update it -
0is the starting value
return <h1>{count}</h1>; → displays the stored value
Now imagine a digital display:
- it remembers the number
- it doesn’t reset unless you tell it to
Now that we can store it… how do we change it?
How to Use useState (The Simple Way)
We need a way to update the value.
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<button> → creates a clickable element
onClick={() => ...} → tells React what to do when clicked
() => setCount(count + 1) →
- takes current value
- adds 1
- sends it to React
setCount(...) → updates the stored value
React sees the update → re-renders the component → new value appears
It’s like pressing a counter device:
- click
- number increases
- display updates instantly
Let’s bring everything together into one working piece.
Let’s Build Something: Counter App
Here’s the full setup:
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h1>{count}</h1>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}
export default Counter;
import { useState } from "react"; → enables state in this component
function Counter() → defines the UI logic
useState(0) → sets starting value
count → holds current number
setCount → updates that number
<h1>{count}</h1> → shows the current value
onClick={() => setCount(count + 1)} →
- user clicks
- value increases
- React updates UI
export default Counter; → allows this component to be used elsewhere
Picture a scoreboard:
- starts at 0
- every click adds a point
- display updates instantly
Now that you’ve seen it working, let’s understand when to use this.
When Should You Use useState?
Use useState when something changes over time and needs to be shown on screen.
Imagine filling out a form:
const [name, setName] = useState("");
useState("") → starts with empty text
name → stores what user types
setName → updates it
When connected to input:
- user types
- value updates
- UI reflects instantly
It’s like typing on your phone—each letter appears immediately.
But not everything needs this kind of memory.
When NOT to Use useState
Sometimes, keeping track is unnecessary.
const title = "Welcome";
const title → simple fixed value
No updates → no re-render needed
Like a printed poster:
- always the same
- no interaction
Now let’s see where it becomes really powerful.
Real-Life Situations Where useState Shines
Here’s where things get exciting.
1. Form Input
<input
value={name}
onChange={(e) => setName(e.target.value)}
/>
value={name} → input shows current state
onChange → listens for typing
e.target.value → gets typed text
setName(...) → updates state
React re-renders → input stays in sync
2. Toggle Feature
const [isOpen, setIsOpen] = useState(false);
<button onClick={() => setIsOpen(!isOpen)}>
Toggle
</button>
isOpen → current state (true/false)
setIsOpen → updates it
!isOpen → flips value
Click → value changes → UI updates
3. Conditional Rendering
{isOpen && <p>Visible now</p>}
isOpen && → checks condition
If true → show content
If false → hide content
State controls visibility.
Now let’s sharpen how we update things.
Pro Tips (That Make You Look Smart)
setCount(prev => prev + 1);
prev → latest value from React
prev + 1 → safe update
Prevents outdated values
Useful when multiple updates happen quickly.
Common Beginner Mistakes
count = count + 1; // ❌ wrong
This changes the variable… but:
- React doesn’t track it
- No re-render happens
- UI stays the same
Correct approach:
setCount(count + 1);
React sees update → re-renders → UI updates
So What Did We Learn?
You started with something that resets every time…
and turned it into something that remembers, updates, and reacts.
Now your UI:
- keeps track of changes
- updates instantly
- feels alive
That’s the magic of useState.
What’s Next? (And Why You Should Care)
Now you know how to store and update values.
But what if:
- something needs to happen automatically after rendering?
- you fetch data from an API?
- you deal with timers or external events?
That’s where the next step comes in…
👉 useEffect
Once you combine both, you move from simple interactions to real-world applications.
Before You Go…
Try this:
👉 Add a decrement button
👉 Stop it from going below 0
👉 Add a reset button
Watch how every action:
- updates the value
- refreshes the UI
- keeps everything in sync
That’s when it really clicks.
Once you start seeing it this way, React stops feeling confusing…
and starts feeling like something you can actually control
This article was originally published by DEV Community and written by Kathirvel S.
Read original article on DEV Community