React/State/Lesson 07
useState
30 min·theory
This chapter
1/2
JavaScript
useState
💡 Why should you learn this?
🎯
This is the most fundamental way to store and update values displayed on the screen in React.
💼
It is essential for responding to user interactions such as button clicks and text input.
⚡
This is the single most important feature that every React developer uses every day.
🏢 실무에서는
In production, useState is used when pressing a 'Like' button increments a count, or when a toggle button opens and closes a menu. The quantity increment/decrement buttons in an online shop are all built with useState.
useState — A component's memory: when the value changes, the screen changes too
Real-world analogy: A Whiteboard
A component has its own whiteboard.
- ▸
state: the value written on the whiteboard - ▸
setState: the act of erasing and writing a new value - ▸When the whiteboard changes → the screen is automatically redrawn
Why learn this?
Changing a regular variable does not update the screen.
You must manage it with useState so React can detect the change and re-render.
Basic usage
Things to watch out for
- ▸Never mutate state directly without setState → the screen will not update
- ▸setState is asynchronous → the state value may not be updated immediately after the call
- ▸Arrays and objects must be replaced with a new reference (immutability)
💡 ⚠️ Common mistakes
- ▸Passing the result of a function call directly as the initial value of useState, causing unnecessary computation on every render
- ▸Referencing current state directly instead of using a functional update when the update depends on the previous state, leading to closure bugs
- ▸Mutating object or array state in place so that React cannot detect the change
💡 🎯 Interview prep
Q: When is a functional update in useState necessary, and why?
Q: Why is state immutability important in React, and how do you implement it?
Hint: Functional updates are needed to resolve closure issues and to safely update based on the previous state. Immutability is important for React's shallow-comparison optimization and predictable state management. Be sure to mention concrete implementation approaches such as immer or the spread operator.
⚛️ React pattern — useState
Learn step by step, with code, how useState is used in React.
1
📦
1. Declare State
Create component-local state with useState
const [count, setCount] = useState(0);
↓
2
👁️
2. Rendering
Display the state value in JSX
return <h1>Current count: {count}</h1>;
↓
3
🔄
3. Update
Call setState → trigger re-render
<button onClick={() => setCount(count + 1)}>+1</button>
↓
4
💡
4. Core principle
state is immutable — never mutate directly; always use the setter
✨ useState — Build a counter
Edit the code directly and it will be reflected automatically after 0.7 seconds. Runs instantly in the browser with React 18 + Babel.
🖥️ Result — rendered React component
✏️ React 코드 수정하기 (클릭해서 열기)
⚛️ React 18 + Babel Standalone — see the result first, then edit the code yourself.
Check your understanding
What happens if you change state directly without using setState?
Read this first: Lists and key
Up next: Form Handling