React/Hooks/Lesson 09
useEffect
35 min·theory
This chapter
1/5
JavaScript
useEffect
💡 Why should you learn this?
🎯
This is the most fundamental way to fetch data from a server.
💼
You can control whether it runs only once on page load or only when a specific value changes.
⚡
It is an essential feature that every React developer uses on a daily basis.
🏢 실무에서는
In real-world projects, when a component appears on screen, `useEffect` is used to call an API and retrieve data such as product lists and user information. Nearly every web app is built around this pattern.
useEffect — Component lifecycle, API calls, and timers
Real-world analogy: an entrance/exit attendant
A Hook that registers code to run automatically when a component appears on screen (mount) and when it disappears (unmount).
Why learn it?
- ▸Data loading (API calls)
- ▸Setting up timers
- ▸Registering/removing event listeners
Basic structure
Dependency array patterns
💡 ⚠️ Common mistakes
- ▸Omitting required values from the dependency array and ignoring ESLint warnings (violation of the exhaustive-deps rule)
- ▸Using
setInterval,addEventListener, etc. without a cleanup function, causing memory leaks - ▸Using
async/awaitdirectly in the useEffect callback (useEffect should only return a cleanup function) - ▸Infinite re-renders caused by reference equality issues when using objects or arrays as dependencies
💡 🎯 Interview prep
Q: Explain the difference between an empty dependency array [] and no dependency array in useEffect.
Q: How can you prevent race conditions when making API calls inside useEffect?
Q: When does the useEffect cleanup function run, and why is it necessary?
Hint: Clearly distinguish the three dependency array patterns (none / empty array / with values), and explain how to resolve race conditions using AbortController or a cleanup flag with code examples. Answer with real-world cases illustrating why cleanup matters for preventing memory leaks.
⚛️ React pattern — useEffect
Learn how useEffect is used in React step by step, with code examples.
1
🎯
1. Defining an Effect
A side effect to run after render
useEffect(() => {
console.log('Mount or count changed');
}, [count]);
↓
2
📋
2. Dependency array
[] = once only, [x] = when x changes, none = every render
↓
3
🧹
3. Cleanup function
Return a cleanup function — runs on unmount
useEffect(() => {
const t = setInterval(...);
return () => clearInterval(t); // cleanup
}, []);
↓
4
⚠️
4. Common mistakes
Missing dependencies → stale closure / infinite loop
⏰ useEffect — mount + dependencies + cleanup
Edit the code directly and changes are 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
When the useEffect dependency array is `[]` (an empty array), when does it run?
Read this first: Form Handling
Up next: useReducer — Foundation of Redux