Question presented to candidate: "Switching tabs replaces the content with a spinner for a moment, and typing in the filter box stutters. How do you fix both without changing the data layer?"
What a strong answer should cover:
- Two different problems that share a solution: the fallback flash (Suspense replacing visible content) and input lag (an expensive render blocking the keystroke).
- Wrapping the update in
startTransitionmarks it non-urgent, and React then keeps the already-visible content on screen instead of showing the Suspense fallback. - That only applies to content that has already been shown. A first load has nothing to keep, so the fallback still appears — which is correct.
isPendinggives you the affordance: dim the stale content, disable the control. Not a spinner replacing it, which recreates the flash you removed.- For a value arriving from elsewhere — a prop, a controlled input —
useDeferredValueis the same idea applied to a value you consume rather than an update you make. - Deferring only helps if the expensive child is memoised, or both renders do the work.
- These reprioritise rendering. If the wait is the network, the fix is fetching earlier — preloading or hoisting the request.
Clarifying questions expected:
- "Is the delay rendering or fetching?" — transitions help the first only.
- "Is there already content on screen, or is this a first load?" — that decides whether the fallback is avoidable at all.
Code / implementation expected: Optional. Showing the same update with and without startTransition is the whole answer.