React / 49
Suspense boundaries and startTransition
Where to put Suspense so a loading chunk doesn't blank your page, the old "suspended while responding to synchronous input" error, and how startTransition keeps the old screen visible.
The nearest boundary wins
When a component suspends (a lazy component whose code isn't loaded yet), React walks up the tree to the nearest <Suspense> and shows its fallback in place of everything inside that boundary.
So placement decides how much disappears:
<>
<Header /> {/* outside the boundary: always stays */}
<Suspense fallback={<TabSpinner />}>
{tab === "menu" ? <Menu /> : <Reviews />} {/* only this area is swapped */}
</Suspense>
</>Keep the frame outside
Header, navigation and tabs sit outside the boundary so they never flicker.One boundary per area that loads together
A page, a tab panel, a heavy widget. You can nest boundaries: an inner one handles its own area, the outer one catches everything else.Not one per component
Ten spinners popping in one by one feels worse than one tidy loading state.
The "synchronous input" error
In React 18 you could hit this error:
A component suspended while responding to synchronous input. This will cause the UI to be replaced with a loading indicator. To fix, updates that suspend should be wrapped with startTransition.
When: an urgent update (a click, typing) caused a component to suspend, and there was no <Suspense> boundary above it to show a fallback. React treats clicks and typing as urgent: it wants to show the result right away, and it couldn't.
Fix A: add a <Suspense> boundary above the lazy component, so React has a fallback to show. Fix B: mark the update as non-urgent with startTransition, so React is allowed to wait.
startTransition: keep the old screen
A transition is an update you mark as not urgent. For Suspense, that changes one important thing: if the new screen isn't ready, React keeps showing the current one instead of hiding already visible content behind a fallback.
setTab("reviews")before
click
ready
startTransition(() => setTab("reviews"))before
click
ready
useTransition also gives you isPending, so you can show a subtle hint (dim the old content, a small spinner on the tab) instead of a big fallback:
import { lazy, Suspense, useState, useTransition } from "react";
import Menu from "./Menu.jsx";
const Reviews = lazy(() => import("./Reviews.jsx"));
export default function RestaurantTabs() {
const [tab, setTab] = useState("menu");
const [isPending, startTransition] = useTransition();
function selectTab(next) {
startTransition(() => setTab(next));
}
return (
<>
<button onClick={() => selectTab("menu")}>Menu</button>
<button onClick={() => selectTab("reviews")}>Reviews {isPending && "…"}</button>
<Suspense fallback={<p>Loading…</p>}>
{tab === "menu" ? <Menu /> : <Reviews />}
</Suspense>
</>
);
}If you don't need isPending, import startTransition directly from react and call it the same way.
Fallback or keep the old screen?
Try it- Masala dosa
- Paneer tikka
- Mango lassi
The transition doesn't wait forever for everything: it only avoids hiding content that's already on screen. A brand-new <Suspense> boundary that appears for the first time still shows its fallback straight away.
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
