Skip to main content
React Interview Questions Predict Output P0 Hard

Mixed state updates in one click

Mount App, then click once. What logs during the handler, and what is the final button text?

Official Reference

Code Snippet

import { useState } from "react";
export default function App() {
  const [n, setN] = useState(2);
  return <button onClick={() => {
    setN(n + 4);
    setN(x => x * 3);
    setN(n - 1);
    setN(x => x + 8);
    console.log(n);
  }}>{n}</button>;
}

Expected Output & Explanation

Console: 2
Button: 9

The handler’s n stays 2. React applies the queue as replace with 6, multiply to 18, replace with 1, then add 8.

Module Context & References

React · Predict the behavior Trace state queues, stale snapshots, keys, effects and refs
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.