Functional State Updates in React
Learn how to update React state using updater functions, why previous state matters, and how functional updates help build reliable interactive applications for CIIT Training Institute.
1. What Are Functional State Updates?
A functional state update passes a function to a React state setter instead of passing the next value directly. React supplies the previous state value to this function, and the function returns the next state value.
This approach is useful when the next state depends on the previous state, such as increasing a counter or adding another completed lesson.
setCount(previousCount => previousCount + 1);
Explanation:
setCountrequests a state update.previousCountreceives the previous queued state value.previousCount + 1calculates the next count.- The returned value becomes the next state value.
2. Direct Update vs Functional Update
React lets you pass either a value or an updater function to a state setter. The best choice depends on how the next value is calculated.
setCount(count + 1);
This calculates the next value from the count captured by the current render.
setCount(previousCount => previousCount + 1);
This calculates the next value from the previous state supplied by React when processing the updater.
| Direct Update | Functional Update |
|---|---|
setCount(count + 1) |
setCount(previousCount => previousCount + 1) |
| Uses the value captured by the current render. | Receives the previous queued state value. |
| Useful when the next value is known or a simple update is appropriate. | Useful when the next value depends on the previous state. |
3. Counter Example Using Functional Updates
SAMADHAN is completing React lessons at CIIT Training Institute. Each time he clicks the button, the completed lesson count increases by one.
import { useState } from "react";
function LessonCounter() {
const [count, setCount] = useState(0);
function completeLesson() {
setCount(previousCount => previousCount + 1);
}
return (
<div>
<h2>Student: SAMADHAN</h2>
<p>Completed Lessons: {count}</p>
<button onClick={completeLesson}>
Complete Lesson
</button>
</div>
);
}
export default LessonCounter;
How it works:
- The initial count is 0.
- SAMADHAN clicks Complete Lesson.
- The updater receives the previous count.
- The updater returns the previous count plus one.
- React renders the new count.
Expected Browser Output
Initially: Completed Lessons: 0
After one click: Completed Lessons: 1
After two clicks: Completed Lessons: 2
4. Why Multiple Updates Matter
React can batch state updates. If multiple updates use the same state value captured by the current render, each update may calculate the same next value.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function increaseThreeTimes() {
setCount(count + 1);
setCount(count + 1);
setCount(count + 1);
}
return (
<div>
<p>Count: {count}</p>
<button onClick={increaseThreeTimes}>
Increase Three Times
</button>
</div>
);
}
export default Counter;
Expected Result for This Example
Initial count: 0
After clicking once: Count becomes 1, not 3.
All three calls use the same count value from the current render. Each requests the value 1.
5. Increase the Count Three Times Correctly
We can use updater functions to calculate each next value from the result of the preceding queued update.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function increaseThreeTimes() {
setCount(previousCount => previousCount + 1);
setCount(previousCount => previousCount + 1);
setCount(previousCount => previousCount + 1);
}
return (
<div>
<p>Count: {count}</p>
<button onClick={increaseThreeTimes}>
Increase Three Times
</button>
</div>
);
}
export default Counter;
Expected Result for This Example
Initial count: 0
After clicking once: Count becomes 3.
Each updater receives the result of the previous queued updater. The sequence is 0 to 1, then 1 to 2, then 2 to 3.
6. Functional Updates in a CIIT Course Dashboard
SAMIKSHA has completed two lessons in a CIIT React course. When she completes another lesson, the dashboard should increase her lesson count using the previous state.
import { useState } from "react";
function StudentDashboard() {
const [completedLessons, setCompletedLessons] = useState(2);
function markLessonComplete() {
setCompletedLessons(
previousLessons => previousLessons + 1
);
}
return (
<div>
<h2>CIIT Student Dashboard</h2>
<p>Student: SAMIKSHA</p>
<p>Completed Lessons: {completedLessons}</p>
<button onClick={markLessonComplete}>
Mark Lesson Complete
</button>
</div>
);
}
export default StudentDashboard;
Expected Browser Output
Student: SAMIKSHA
Initially: Completed Lessons: 2
After clicking: Completed Lessons: 3
7. Functional Update Flow Diagram
This diagram shows how React processes functional state updates.
8. Important Rules for Updater Functions
- Return the next state value from the updater function.
- Do not mutate the previous state directly.
- Keep updater functions pure: do not perform side effects inside them.
- Use functional updates when the next value depends on previous state.
- Do not expect a setter to update the current render's variable immediately.
setCount(previousCount => previousCount + 1);
9. Common Mistakes
Mistake 1: Repeating a direct update
setCount(count + 1); setCount(count + 1); setCount(count + 1);
Correct approach:
setCount(previousCount => previousCount + 1); setCount(previousCount => previousCount + 1); setCount(previousCount => previousCount + 1);
Mistake 2: Mutating state
Do not modify objects or arrays in place. Return a new object or array when updating structured state.
Mistake 3: Performing side effects in an updater
Keep updater functions limited to calculating and returning the next state value.
10. Practice Exercises
- Create a counter that increases three times on one click.
- Start SAMADHAN's completed lesson count at 4 and increment it.
- Update SAMIKSHA's course progress using a functional updater.
- Create a counter that decreases using previous state.
- Explain why three direct updates can produce only one increment.
- Explain when a functional state update is useful.
11. Quick Revision
- Functional updates pass a function to a state setter.
- React supplies the previous state value to the updater.
- The updater returns the next state value.
- Functional updates are useful when updates depend on previous state.
- Updater functions should be pure.
Summary
Functional state updates help calculate the next state from the previous state. They are particularly useful when multiple updates are queued or when the next value depends on the latest state. For example, setCount(previousCount => previousCount + 1) calculates an increment using the previous value supplied by React. Always keep updater functions pure and return the next state instead of mutating the existing state.