Why this matters
React is the most widely used UI library and the default for AI product frontends. Most React bugs come from misunderstanding when components re-render and what effects are for.
Core ideas
- Components are functions that return UI from props (inputs) and state (memory).
- Rendering: when state or props change, React calls the component again and updates the DOM to match. Rendering should be pure: no side effects.
- State updates are asynchronous and immutable:
setItems([...items, item]), neveritems.push(item). - Effects (
useEffect) synchronise with things outside React: subscriptions, timers, non-React widgets. They are not for deriving data from props or state. - Keys identify list items across renders; use stable IDs, not array indexes.
- Lifting state up: shared state lives in the closest common parent; context for truly global things (theme, current user).
How to do it
type Status = "idle" | "loading" | "error" | "done";
function AskBox({ onAsk }: { onAsk: (q: string) => Promise<string> }) {
const [question, setQuestion] = useState("");
const [status, setStatus] = useState<Status>("idle");
const [answer, setAnswer] = useState("");
async function submit(e: React.FormEvent) {
e.preventDefault();
setStatus("loading");
try { setAnswer(await onAsk(question)); setStatus("done"); }
catch { setStatus("error"); }
}
return (
<form onSubmit={submit}>
<textarea value={question} onChange={(e) => setQuestion(e.target.value)} />
<button disabled={status === "loading" || !question.trim()}>Ask</button>
{status === "error" && <p role="alert">Couldn't get an answer. Try again.</p>}
{status === "done" && <p>{answer}</p>}
</form>
);
}
Derived values (like "is the button disabled") are computed during render, not stored in state or synced with effects.
Why this way
Pure rendering + immutable updates make components predictable and fast to diff. One status value (or a discriminated union) instead of several booleans prevents contradictory states like loading && error.
Common mistakes
useEffectto copy props into state, causing stale or doubled renders.- Missing dependencies in effect arrays (stale closures).
- Index keys in lists that reorder, mixing up item state.
- Fetching in effects without handling races (the slower old request overwriting the newer one). Use a data library or abort controllers (4.4).
Interview follow-ups
- "Why does my component render twice in development?" React Strict Mode intentionally double-invokes renders and effects to expose impure code; production renders once.
- "When do you reach for
useMemo/useCallback?" When profiling shows an expensive computation or a memoised child re-rendering needlessly; not by default (React's compiler increasingly handles this). - "Context or a state library?" Context for low-frequency global values (theme, user); a store or a server-state library for frequently changing or server data, to avoid re-rendering the whole tree.
- "How do you test components?" Testing Library from the user's perspective (roles, text, events), mocking the network (MSW), not implementation details.
Check yourself
When should you use `useEffect`?
To synchronise with something outside React (subscriptions, timers, imperative widgets, browser APIs). Not to compute values from props or state; compute those during render.
Why not store `isLoading`, `isError` and `data` as three separate states?
They can contradict each other. A single status (or a discriminated union with data attached) makes impossible states unrepresentable.
Build it
Build the AskBox with a fake onAsk that sometimes fails, and Testing Library tests for loading, error and success.