Question presented to candidate: "Some data in our dashboard changes while the user is on the page and needs refetching. How do you fetch in a Client Component, and when would you do it on the client at all instead of on the server?"
What a strong answer should cover:
- Prefer server fetching for the initial data; use the client for data that changes while the page is open, depends on client state, or is user-triggered.
- Use a library: SWR or TanStack Query give caching, request deduplication, revalidation on focus, retries and mutations.
- Avoid raw
useEffect+fetch: it has race conditions, no cache, and double-fetches in Strict Mode development. - To stream server data into a client component, pass a Promise from a Server Component and read it with React
use()inside Suspense. - Client-fetched data is not in the server HTML, so it does not help SEO or the first paint.
Clarifying questions expected:
- "Does this data need to be in the first HTML?" β if so, fetch on the server.
- "Does it change while the user is looking?" β that is where a client library earns its place.
Code / implementation expected: Yes β an SWR hook, and a server promise unwrapped with use().