Skip to main content
React Interview Questions Predict Output P0 Hard

Effect setup and cleanup across commits

Without development Strict Mode, mount, wait for effects, click Switch, wait for effects, then click Remove. List the console logs.

Official Reference

Code Snippet

import { useEffect, useState } from "react";
function Room({ id }) {
  useEffect(() => {
    console.log("connect", id);
    return () => console.log("disconnect", id);
  }, [id]);
  return <p>{id}</p>;
}
export default function App() {
  const [id, setId] = useState("A");
  const [show, setShow] = useState(true);
  return <>
    {show && <Room id={id} />}
    <button onClick={() => setId("B")}>Switch</button>
    <button onClick={() => setShow(false)}>Remove</button>
  </>;
}

Expected Output & Explanation

connect A
disconnect A
connect B
disconnect B

The old cleanup closes over A and runs before the new B setup. Unmount then runs B’s cleanup.

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.