Skip to content
Source
Sign inSign up
TanStack/router

lazyRouteComponent skips use() after the import resolves, triggering React 19's "conditional use()" warning #8487

Openalebedev(GitHub profile, opens in a new tab) opened this issue 3 comments

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

  1. npm install && npm run dev
  2. Open http://localhost:3000 with the DevTools console open.
  3. 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)
  1. Confirming this on @tanstack/react-router 1.170.36 with react/react-dom 19.3.0 (a Vite SPA, no Start). 1.170.40 still has the same Lazy code.

    We see it outside the race described above, too. If a lazyRouteComponent is rendered inside a route's component, rather than being the route's component itself, 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 marked fulfilled. More detail and a note on reproducing it in Vitest: https://github.com/TanStack/router/pull/8549#issuecomment-5916390699

  2. Also reproduced with @tanstack/react-router 1.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 conditional use() behavior described in this issue.

  3. Another path that hits this on a single cold navigation, with no transition race and no nested lazy component: a code-split pendingComponent with defaultPendingMs: 0 and an async beforeLoad. offerPending renders the pending component while beforeLoad is still running, but loadRouteChunk (called from createLoaderTask, after contextualize finishes) hasn't requested its chunk yet. So Lazy suspends through use() on mount, then renders without use() once the import resolves.

    Versions: @tanstack/react-router 1.170.46 (@tanstack/router-core 1.171.39), react/react-dom 19.3.0, vite 8.3.5, @vitejs/plugin-react 6.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,
    });
    1. Render <RouterProvider router={router} /> in a Vite dev build and open /.
    2. Click Slow. The warning is logged on the first navigation in every fresh document.

    With defaultPendingMs: 1000, or with Pending.preload() awaited before navigating, it doesn't warn. Applying #8549's change to lazyRouteComponent in node_modules also removes the warning on this path.

Sign in to join this conversation.