Skip to main content
React Interview Questions Predict Output P0 Hard

Changing a prop with or without a changing key

Click A: 0 to increment it, then click Switch. Compare the first button if the marked key is removed and the same steps are repeated after a fresh mount.

Official Reference

Code Snippet

import { useState } from "react";
function Counter({ label }) {
  const [n, setN] = useState(0);
  return <button onClick={() => setN(x => x + 1)}>{label}: {n}</button>;
}
export default function App() {
  const [label, setLabel] = useState("A");
  return <>
    <Counter key={label} label={label} />
    <button onClick={() => setLabel("B")}>Switch</button>
  </>;
}

Expected Output & Explanation

With key: B: 0
Without key: B: 1

A different key means a different component identity. Without key, the same type occupies the same position after the prop changes, so its state survives.

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.