Interview ยท Topic 22
๐Quick-Reference Cheat Sheets
22.1Performance โ do these in order under deadline
- Profile (DevTools Profiler + Lighthouse + bundle analyzer) โ find the real bottleneck.
- Route-based code splitting (
lazy+Suspense). React.memoon the top few heavy components.useCallbackon callbacks passed to those memoized children;useMemoon expensive derived values.useDeferredValue/useTransitionfor search/filter/heavy updates.- Virtual scroll (
react-window) for lists > ~100 items. - react-query/SWR for server state (caching, dedup, retries).
- Web Vitals monitoring + error boundaries before launch.
22.2Core Web Vitals (2024+)
- LCP < 2.5s (loading) โ hero image, TTFB, render-blocking resources.
- INP < 200ms (responsiveness) โ replaced FID in March 2024; break long tasks, reduce main-thread work.
- CLS < 0.1 (stability) โ set dimensions, reserve space,
transformfor animations.
22.3When to reach for what (state)
- Local UI โ
useState - Low-frequency app-wide (theme/auth/locale) โ Context (split, don't monolith)
- High-frequency cross-component โ Zustand (selector subscriptions)
- Complex flows + big team + time-travel โ Redux Toolkit
- Server data โ react-query/SWR (not Redux/Zustand)
22.4Rendering strategies (Next.js)
- Static, rare change โ SSG (
revalidatedaily) - Dynamic, periodic โ ISR (hourly) + on-demand revalidation for CMS
- Per-user/auth โ SSR + client cache
- Real-time โ CSR/streaming/websockets
- Default modern โ RSC (server components) + client leaves for interactivity
22.5Memoization defeaters (why React.memo "isn't working")
- Inline function props (
onClick={() => โฆ}) โ new reference each render โuseCallback. - Inline object/array props (
style={{โฆ}},data={[โฆ]}) โ new reference โuseMemo. - Passing
childrenthat change identity each render. - Spreading new objects into props.
22.6Security one-liners
- XSS โ React escapes by default; sanitize
dangerouslySetInnerHTMLwith DOMPurify; add CSP. - CSRF โ
SameSitecookies + CSRF tokens + Origin checks. - Secrets โ server-only; only
NEXT_PUBLIC_reaches the client; browser โ your backend โ third-party. - Fix XSS first โ it can defeat CSRF defenses.
22.7Refactoring checklist
- Characterization tests green (behavior locked)
- Profiler baseline recorded
- Extract: pure functions โ hooks โ leaf components (innermost first)
- Test + commit after every extraction
- Public API unchanged
- Re-profile: even or better
- Ship incrementally; app deployable at every commit