Skip to content

Commit 2e079d4

Browse files
committed
Fix race condition with rendering and layout effect subscription during hydration
1 parent 89e2bfe commit 2e079d4

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

packages/react-router/lib/components.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,16 @@ export function RouterProvider({
667667
// pick up on any render-driven redirects/navigations (useEffect/<Navigate>)
668668
React.useLayoutEffect(() => router.subscribe(setState), [router, setState]);
669669

670+
// Track race conditions where we finish initializing prior to the layout
671+
// effect above running to register our listener. If we manually detect a
672+
// change in `state.initialized`, automatically sync state.
673+
let initialized = state.initialized;
674+
React.useLayoutEffect(() => {
675+
if (!initialized && router.state.initialized) {
676+
logErrorsAndSetState(router.state);
677+
}
678+
}, [initialized, logErrorsAndSetState, router.state]);
679+
670680
// When we start a view transition, create a Deferred we can use for the
671681
// eventual "completed" render
672682
React.useEffect(() => {

0 commit comments

Comments
 (0)