Skip to content

State Updates and Batching in React

How React batches state updates and what it means for setState and derived state.

Rajesh DhimanFebruary 08, 20262 min read
On this page

30‑second interview answer

React may batch multiple state updates into a single re-render to improve performance. When the next state depends on the previous state, you should use the functional updater form: setState(prev => next). Avoid storing duplicate/derived state; keep one source of truth and derive values during render.


What is batching?

Batching means:

  • multiple updates happen
  • React performs one render instead of many

This makes UI faster and prevents unnecessary work.

In modern React (React 18), batching happens in more places than before.


The most important rule: use functional updates

Bad when you depend on previous state:

setCount(count + 1);
setCount(count + 1);

This can produce +1 instead of +2 because both lines captured the same count.

Good:

setCount((c) => c + 1);
setCount((c) => c + 1);

Now the second update receives the updated value.


Derived state pitfalls

Anti-pattern: storing derived values

const [fullName, setFullName] = useState(first + ' ' + last); // ❌

If first or last change, fullName can drift.

Better:

const fullName = `${first} ${last}`;

Keep source of truth. Derive in render.


Updates are async-ish (don’t rely on immediate state)

setState schedules an update. Don’t expect state to change immediately in the same call stack.

If you need to react to state changes, use useEffect.


When multiple state variables depend on each other

If you find yourself doing many state variables that change together, consider useReducer.

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return { ...state, count: state.count + 1 };
    default:
      return state;
  }
}

Reducers can make updates more predictable.


Interview trap: “Does React always batch?”

Safe answer:

“React batches in many cases, especially inside React event handlers and in React 18 more broadly. But I still write updates using functional updaters when they depend on previous state.”


Mini Q&A

Q1: Why use functional updates?

  • To avoid stale values when multiple updates are batched.

Q2: What’s derived state?

  • State that can be computed from other state/props.

Q3: When use useReducer?

  • When state transitions are complex or multiple updates need to be coordinated.

Summary checklist

  • I can define batching.
  • I use functional updates when needed.
  • I avoid derived state duplicates.
  • I know when to use useReducer.
Companion Book

Looking for structured, offline interview prep?

Free online lessons • Portable Amazon Kindle / Paperback

Frontend Interview Guide (2025 Edition) cover
Recommended for React & Frontend Rounds

Frontend Interview Guide (2025 Edition)

By Rajesh Dhiman

Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.

View on Amazon ↗Kindle & Paperback
JavaScript Interview Guide cover
Recommended for Core JS Rounds

JavaScript Interview Guide

By Rajesh Dhiman

Deep dive into the Event Loop, microtask draining, closures, prototypal inheritance, and asynchronous execution patterns.

View on Amazon ↗Kindle & Paperback

Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate, we may earn from qualifying purchases.

RD

Written by Rajesh Dhiman

Authored by Rajesh Dhiman and engineered at Eunix Tech. Focused on deep-dive frontend engineering, JavaScript runtime internals, React architecture, and web performance.