State management and data fetching · mid
URL as state: filters, search and pagination
Shareable view state (filters, sort, page, search query) should live in the URL query string as the single source of truth, parsed and validated into typed state, and updated with replace while typing and push for committed changes.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- The URL is the source of truth; do not keep a second copy in
useStatesynced with two effects (that causes loops). - Parse and validate: unknown sort values or negative pages fall back to defaults (zod or a small parser;
nuqsis a popular helper in Next.js). - Use
router.replace(orhistory.replaceState) for every keystroke andpushwhen the user commits a filter, so Back works sensibly. Debounce URL writes for typing. - Keep ephemeral UI (open dropdowns, hover) out of the URL.
- Remove default values from the URL and keep a stable parameter order so URLs (and caches) are canonical.
- Scroll restoration on Back: cached list data plus the router's or browser's restoration; virtualised lists need an anchor index.
- In Next.js,
searchParamsis request-time data: read it inside<Suspense>, and pass the values into"use cache"functions so they become cache keys.
The red pen
Common traps: mistake → fix
Mistake: "Sync URL and state with two effects"
Fix: infinite loops and double renders.
Mistake: "Push a history entry per keystroke"
Fix: Back becomes useless.
Mistake: "Trust URL params"
Fix: they are user input; validate them.
Easy to hard
Questions interviewers ask, easy to hard
- Filters, sort and page live in React state, so Back loses them and links are not shareable. Redesign it.
pushvsreplacefor each keystroke?- How do you restore scroll when coming back from the product page?
- In Next.js, how does reading
searchParamsaffect caching and Suspense?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.