Skip to content

Designing UI

Writing a component that works is easy. Designing one that other people can use, that behaves correctly with a keyboard, and that does not collapse under a large list is the actual skill. The questions below run roughly in order of how often they decide a front-end interview.

“Design components around composition, not configuration: take children and slots instead of adding boolean props. Keep props small, explicit and typed, and let the parent own state. Accessibility is not optional — semantic elements, labels, keyboard handling, visible focus, and ARIA only where semantics fall short. Lists need stable keys; forms need one source of truth per field and validation next to the field. For performance, measure first: memoise only provable hot spots, split routes rather than micro-components, and defer work you do not need on the critical path. And every UI needs designed loading, empty and error states, not just the happy path.”

card.tsx
// Configuration creep: every new need adds a boolean.
<Card title="User" showAvatar showFooter footerText="..." dense />
// Composition: the caller decides what goes in the slots.
<Card>
<Card.Header><Avatar src={user.avatar} />{user.name}</Card.Header>
<Card.Body>{user.bio}</Card.Body>
<Card.Footer><Button>Follow</Button></Card.Footer>
</Card>
  • Accept children and named slots (header, footer, actions) rather than flags.
  • Extend the native element with React.ComponentPropsWithoutRef<'button'> instead of re-declaring its props.
  • Forward ref where the component wraps a DOM node (in React 19, ref is a normal prop).
  • Spread unknown props onto the underlying element so aria-*, data-* and handlers work.

Every prop is a contract you maintain, and every boolean doubles the number of states you must render correctly. Keep the public surface small.

disclosure.tsx
function Disclosure({ title, children }: { title: string; children: ReactNode }) {
const [open, setOpen] = useState(false);
const id = useId();
return (
<div>
<button type="button" aria-expanded={open} aria-controls={id}
onClick={() => setOpen((o) => !o)}>
{title}
</button>
<div id={id} hidden={!open}>{children}</div>
</div>
);
}
  • Use the right element. A clickable div needs a role, tab index and Enter/Space handling — a button already has all of it.
  • Label every input with <label htmlFor> or aria-label; placeholder text is not a label.
  • Never remove focus outlines. Restyle them; keyboard users need to see where they are.
  • Manage focus on route and modal changes, and trap it inside a modal.
  • Announce dynamic changes with aria-live="polite".
  • Respect prefers-reduced-motion, keep contrast at WCAG AA, and test with a keyboard and a screen reader. eslint-plugin-jsx-a11y catches the mechanical errors.
uncontrolled.tsx
// Uncontrolled: the DOM owns the value; read it on submit. Fewer re-renders.
<form onSubmit={(e) => {
e.preventDefault();
onSearch(String(new FormData(e.currentTarget).get('q') ?? ''));
}}>
<label htmlFor="q">Search</label>
<input id="q" name="q" type="search" />
</form>
controlled.tsx
// Controlled: React owns the value; needed for live validation and derived UI.
const invalid = email.length > 0 && !email.includes('@');
<input id="email" type="email" value={email} aria-invalid={invalid}
aria-describedby={invalid ? 'email-error' : undefined}
onChange={(e) => setEmail(e.target.value)} />
{invalid && <p id="email-error" role="alert">Enter a valid email</p>}

One source of truth per field; validate on blur or submit, not on every keystroke; disable submit only when the reason is obvious; show errors next to the field and wire them with aria-describedby. For complex forms, a schema (Zod) plus a form library shares validation with the server.

  • Keys must be stable, unique among siblings, and not the array index unless the list is static. Keys are not props — a component cannot read key, so pass the id separately if it needs it.
  • Virtualise long lists (@tanstack/react-virtual) rather than rendering thousands of rows.

The performance order matters: measure, then fix the actual bottleneck.

  1. Render less. Move state down, split components so a keystroke does not re-render a tree, pass children through unchanged so they keep identity.
  2. Defer non-urgent work. useDeferredValue for search results, useTransition for expensive transitions.
  3. Memoise only what profiling shows. memo for components re-rendering with identical props, useMemo for expensive computation, useCallback when a function identity is a dependency.
  4. Split the bundle by route, not by micro-component, with React.lazy plus Suspense or the framework’s route-level splitting.
  5. Load assets well. loading="lazy" for below-the-fold images, explicit dimensions to avoid layout shift.
deferred-search.tsx
function Search({ query }: { query: string }) {
const deferred = useDeferredValue(query);
const results = useMemo(() => filterAll(deferred), [deferred]);
return <Results items={results} stale={deferred !== query} />;
}

Every asynchronous UI has at least four states: loading, empty, error, success. Design them.

if (isPending) return <ListSkeleton rows={5} />;
if (error) return <ErrorPanel message={error.message} onRetry={refetch} />;
if (!data.length) return <EmptyState action={<Button>Create one</Button>} />;
return <List items={data} />;

Skeletons should match the final layout to avoid a shift. Errors need a recovery path, not just a message. Empty states should explain what would appear and how to make it appear. Wrap route-level subtrees in error boundaries; an unhandled render error otherwise blanks the app.

  1. How do you make a custom dropdown keyboard-accessible?
  2. When is a controlled input the wrong choice?
  3. Why is React.memo sometimes slower than no memo at all?
  4. How would you render 50,000 rows?
  5. What is the difference between useDeferredValue and debouncing?
  • Using index as a key in a sortable list. State and focus attach to the wrong row.
  • aria-label on a div that should be a button. Semantics first, ARIA second.
  • Disabling submit with no explanation. Users cannot tell what is wrong.
  • Memoising everything by default. Cost, stale closures, no measured benefit.
  • Only designing the happy path. Loading and error states are where products feel unfinished.
  • Fetching in useEffect without cancellation. State after unmount, and races where an older response overwrites a newer one.