惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
雷峰网
雷峰网
量子位
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
T
Tailwind CSS Blog
月光博客
月光博客
博客园 - 【当耐特】
博客园_首页
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
React Hooks Made Simple: Mastering useState (Without Losi...
Kathirvel S · 2026-04-28 · via DEV Community

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>;
}

Enter fullscreen mode Exit fullscreen mode

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>;
}

Enter fullscreen mode Exit fullscreen mode

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);

  • count holds the current value
  • setCount is used to update it
  • 0 is 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>

Enter fullscreen mode Exit fullscreen mode

<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;

Enter fullscreen mode Exit fullscreen mode

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("");

Enter fullscreen mode Exit fullscreen mode

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";

Enter fullscreen mode Exit fullscreen mode

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)}
/>

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

isOpen → current state (true/false)
setIsOpen → updates it
!isOpen → flips value

Click → value changes → UI updates


3. Conditional Rendering

{isOpen && <p>Visible now</p>}

Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

This changes the variable… but:

  • React doesn’t track it
  • No re-render happens
  • UI stays the same

Correct approach:

setCount(count + 1);

Enter fullscreen mode Exit fullscreen mode

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