Skip to main content
React Interview Questions Predict Output P1 Medium

Ref changes without a render request

Click Count twice, then Show. What does the console print, and when does the Show button’s label change?

Official Reference

Code Snippet

import { useRef, useState } from "react";
export default function App() {
  const hits = useRef(0);
  const [shown, setShown] = useState(0);
  return <>
    <button onClick={() => console.log(++hits.current)}>Count</button>
    <button onClick={() => setShown(hits.current)}>Show: {shown}</button>
  </>;
}

Expected Output & Explanation

Console: 1, then 2
Show label: Show: 0 until Show is clicked, then Show: 2

Ref writes persist but do not request rendering. Show reads the current ref in an event handler and copies it into state, which updates the UI.

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.