Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-crabs-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Fix a potential race condition that can occur when rendering a `HydrateFallback` and initial loaders land before the `router.subscribe` call happens in the `RouterProvider` layout effect
10 changes: 10 additions & 0 deletions packages/react-router/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,16 @@ export function RouterProvider({
// pick up on any render-driven redirects/navigations (useEffect/<Navigate>)
React.useLayoutEffect(() => router.subscribe(setState), [router, setState]);

// Track race conditions where we finish initializing prior to the layout
// effect above running to register our listener. If we manually detect a
// change in `state.initialized`, automatically sync state.
let initialized = state.initialized;
React.useLayoutEffect(() => {
if (!initialized && router.state.initialized) {
logErrorsAndSetState(router.state);
}
}, [initialized, logErrorsAndSetState, router.state]);

// When we start a view transition, create a Deferred we can use for the
// eventual "completed" render
React.useEffect(() => {
Expand Down