Explain engineering decisions in an interview
Explain behavior before naming tools
In an interview, clarify the inputs, expected outcome and constraints. State the relevant React contract, predict visible behavior, then propose a small test. When discussing a real project, distinguish what you implemented from what you designed or studied. Do not invent production experience. For state questions, remember each render has its own snapshot and eligible queued updater functions compose.
// Interview prediction model, not the React scheduler.export function applyEligible(initial, updates) { return updates.reduce((value, update) => typeof update === 'function' ? update(value) : update, initial);}console.log(applyEligible(2,[3,3])); // 3console.log(applyEligible(2,[n=>n+1,n=>n+1])); // 4The first trace yields 3: two requests replace the value with 3. The second yields 4: two updaters each increment the previous value. In a real component verify the final status after the click, not a fixed number of renders. The model omits scheduling and batching boundaries.
Diagnose stale closure and cleanup
Imagine a clock subscription created once with a callback that calls setTicks(ticks + 1). The callback can retain the original value and subsequent ticks request the same replacement. If the callback only needs to increment, use a functional updater. If the external source changes, the Effect must re-subscribe and clean up its prior connection. Do not simply suppress the dependency linter.
import React, {useEffect, useState} from 'react';export default function ShiftClock({clock}) { const [ticks,setTicks] = useState(0); useEffect(() => { const unsubscribe = clock.subscribe(() => setTicks(n => n+1)); return unsubscribe; }, [clock]); return <p role="status">Ticks: {ticks}</p>;}Supply a stable clock object whose subscribe method accepts a callback and returns an unsubscribe function. Starting at zero, two emitted ticks should eventually display 2. Changing clock unsubscribes from the old source; unmount cleans up the current one. The example retains accumulated ticks when the source changes, an explicit product decision. Test repeated emissions and old-source cleanup in a real React fixture.
Discuss data and form architecture
// A bounded decision aid, not a universal architecture rule.export function suggestOwner({shareableUrl, remoteData, siblingConsumers}) { if (shareableUrl && remoteData) return 'URL for filters; server cache for results'; if (remoteData) return 'server cache'; if (shareableUrl) return 'URL'; if (siblingConsumers) return 'nearest common parent'; return 'local component';}A local disclosure can own its own state; sibling controls may need a common parent; a shareable filter belongs in the URL; remote records may benefit from a server-state cache. Filters and remote results can have different owners. For a form, explain input validation, pending state, error announcements and why the server must enforce permissions. Discuss simpler alternatives before adding global state, and measure performance before optimizing. Practice: describe a reservation form with a rejected submission, keyboard recovery, server authorization and a test plan. There is no single correct library choice independent of requirements.