React interview questions
Show answer
Keys let React match items between renders so it can keep the right component state and DOM nodes when items are added, removed or re-ordered. Use a stable unique id from your data. Array indexes break when the order changes — state (like an input's text) can end up on the wrong row.
Show answer
Effects are for synchronising with something outside React (subscriptions, timers, the DOM, network). Do not use them to compute values from props or state (calculate during render or use
useMemo), to reset state when a prop changes (use akey), or to respond to a user event (do it in the event handler). Fewer effects means fewer extra renders and bugs.- intermediate
You call setCount(count + 1) three times in one click handler. What is the result and how do you fix it?
Show answer
It increases by one, because all three calls read the same
countfrom that render and updates are batched. Use the updater form,setCount(c => c + 1), which queues each update against the latest value. Show answer
Server Components render on the server and send their output (not their code) to the client, so they can read databases or secrets directly and add no JavaScript to the bundle. They cannot use state, effects, browser APIs or event handlers — those belong in Client Components marked with
'use client'. You pass serialisable props from server to client components.