lazyRouteComponent skips use() after the import resolves, triggering React 19's "conditional use()" warning #8487
Which project does this relate to?#
Router (also reproduces under Start)
Describe the bug#
On the client, lazyRouteComponent clears loadPromise in its import .then, and Lazy only calls use() while comp is unset. If React restarts the render on a fiber that suspended through use(), Lazy now skips use(), and React 19 logs:
This library called use() to suspend in a previous render but did not call use() when it finished. This indicates an incorrect use of use(). Learn more: https://react.dev/warnings/conditional-use-of-use
This happens when a transition suspends on a lazy route component inside a Suspense boundary that is already showing content, the import resolves, and a second transition lands before React retries.
Your Example Website or App#
https://github.com/alebedev/tanstack-lazy-use-repro
Steps to Reproduce the Bug or Issue#
npm install && npm run dev- Open http://localhost:3000 with the DevTools console open.
- Click Run repro.
src/routes/index.tsx holds the import behind a gate so the timing is deterministic: a transition renders <Lazy /> (it suspends), then the gate opens and a second startTransition runs in the same tick.
Expected behavior#
No warning. Lazy should call use() on every render that follows one where it suspended through use().
Actual behavior#
The error above is logged every time (3/3 runs in headless Chromium; npm run check in the repo automates it).
Proposed fix#
Keep the fulfilled promise on the client, and call use() on it after it has resolved. React set status = 'fulfilled' when it tracked the thenable, so this use() returns synchronously and never suspends an already loaded component:
loadPromise = importer()
.then((res) => {
if (!(isServer ?? typeof window === 'undefined')) {
- loadPromise = undefined
;(lazyComp as any).preload = undefined
}
comp = res[exportName ?? 'default']
})
@@
} else {
throw load()
}
+ } else if (reactUse && (loadPromise as any)?.status === 'fulfilled') {
+ reactUse(loadPromise)
}
With this applied to node_modules, the repro no longer warns and the component still renders. We've been running it as a local patch in production. Happy to open a PR.
Platform#
- @tanstack/react-router: 1.170.39
- @tanstack/react-start: 1.168.58
- react / react-dom: 19.3.0
- vite: 8.3.0
- Browser: Chromium (Playwright 1.62)
Also reproduced with
@tanstack/react-router1.170.46 and React/React DOM 19.3.1 in a client Vite SPA. A lazy screen rendered inside the route component logs the warning on a cold load:const Screen = lazyRouteComponent(() => import('./Screen')) const Wrapper = () => <Screen /> const route = createRoute({ getParentRoute: () => rootRoute, path: '/example', component: Wrapper, })The wrapper does not expose
Screen.preload, so the router mounts the lazy screen before its import resolves. Passing that public preload function through the wrapper makes the router await it and avoids the warning on this cold-load path. The helper still has the conditionaluse()behavior described in this issue.Another path that hits this on a single cold navigation, with no transition race and no nested lazy component: a code-split
pendingComponentwithdefaultPendingMs: 0and an asyncbeforeLoad.offerPendingrenders the pending component whilebeforeLoadis still running, butloadRouteChunk(called fromcreateLoaderTask, aftercontextualizefinishes) hasn't requested its chunk yet. SoLazysuspends throughuse()on mount, then renders withoutuse()once the import resolves.Versions:
@tanstack/react-router1.170.46 (@tanstack/router-core1.171.39),react/react-dom19.3.0,vite8.3.5,@vitejs/plugin-react6.1.2.// pending.jsx: export default () => <p>Loading…</p> const Pending = lazyRouteComponent(() => import("./pending.jsx")); const rootRoute = createRootRoute({ component: () => ( <> <Link to="/">Home</Link> <Link to="/slow" preload={false}>Slow</Link> <Outlet /> </> ), }); const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: "/", component: () => <p>Home</p> }); const slowRoute = createRoute({ getParentRoute: () => rootRoute, path: "/slow", beforeLoad: () => new Promise((r) => setTimeout(r, 200)), loader: () => new Promise((r) => setTimeout(r, 200)), pendingComponent: Pending, component: () => <p>Slow page</p>, }); const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, slowRoute]), defaultPendingMs: 0, });- Render
<RouterProvider router={router} />in a Vite dev build and open/. - Click Slow. The warning is logged on the first navigation in every fresh document.
With
defaultPendingMs: 1000, or withPending.preload()awaited before navigating, it doesn't warn. Applying #8549's change tolazyRouteComponentinnode_modulesalso removes the warning on this path.- Render
Sign in to join this conversation.
Confirming this on
@tanstack/react-router1.170.36 withreact/react-dom19.3.0 (a Vite SPA, no Start). 1.170.40 still has the sameLazycode.We see it outside the race described above, too. If a
lazyRouteComponentis rendered inside a route'scomponent, rather than being the route'scomponentitself, the router never calls its.preload(). It then suspends on first mount on every cold load, and React logs the warning each time. For us that is the login page: 3/3 cold loads warned, both against a mock API and a real backend.Applying the approach from #8549 as a local helper made the warning go away for us. The helper calls
use()on every render and keeps the resolved promise markedfulfilled. More detail and a note on reproducing it in Vitest: https://github.com/TanStack/router/pull/8549#issuecomment-5916390699