Repository navigation
fix(react-router): don't block a second time when the destination route updates search params on mount - #15538
Conversation
…te updates search params on mount useBlocker's blocker-function registration ran in a passive useEffect, which flushes after passive effects lower in the tree. When a blocked navigation was confirmed via blocker.proceed() and the destination route called setSearchParams in its own mount effect, that mount effect could fire before the parent's registration effect updated the router with the new (no-longer-blocking) predicate -- so the router evaluated the stale, still-blocking function against the setSearchParams-triggered navigation and blocked it again. Move the registration to a layout effect. Layout effects for the whole tree flush before any passive effects do, so the router always has the current blocker function registered before another component's mount effect can trigger a navigation. Root-caused via a debug trace showing the exact order: the child's setSearchParams-triggered shouldBlockNavigation check ran, and only after that did the parent's registration effect fire with the updated predicate. Verified the fix eliminates the race in the same trace. Fixes remix-run#11144
…layout effect After rebasing onto main, the layout effect ran before the key effect's cleanup when the router was recreated, so the previous router's key was registered on the new router. Track the router the key was assigned for and skip registration until the key effect has caught up. Also unmount the root in the new test and shorten the change file.
2d2213c to
5b34082
Compare
Cover StrictMode, router recreation, Activity, and server rendering. Tighten the effect timing comment and patch change note.
|
Thanks for the fix! I added a follow-up in I also shortened the effect comments and patch note. The focused blocker and static-router suites passed locally on the main-based version, and the four files in this follow-up match those tested files byte for byte. Authored by an AI agent on behalf of @brophdawg11 |
brophdawg11
left a comment
There was a problem hiding this comment.
The layout-effect update gives destination mount effects the current blocker predicate. The router and key checks preserve blocker cleanup, and the follow-up covers Strict Mode and server rendering. I verified that merging main preserved the reviewed patch.
Authored by an AI agent on behalf of @brophdawg11
Problem
Fixes #11144.
When a blocked navigation is confirmed with
blocker.proceed(), and the destination route callssetSearchParamsin its own mount effect,useBlockerfires a second, incorrect block — even though the blocking condition is no longer true.Root cause
useBlockerregisters its current blocker-predicate function with the router in aReact.useEffect(a passive effect). Passive effects for a whole React tree flush strictly after all layout effects, but relative to each other, a child's passive effect can fire before its parent's passive effect within the same commit.When a navigation completes and both of these happen in the same commit:
shouldBlockfrom the current route, which just changed), andsetSearchParams)...the child's mount effect can run before the parent's registration effect has a chance to update the router with the new, no-longer-blocking predicate. The router evaluates the stale function against the second navigation and blocks it again.
Confirmed this exact ordering with a temporary debug trace:
Fix
Change the blocker-registration effect in
useBlockerfromuseEffecttouseLayoutEffect. Layout effects for the entire tree flush before any passive effects do, so the router is guaranteed to have the current predicate registered before any component's mount-time passive effect (like the destination route'ssetSearchParamscall) can trigger a navigation check.This matches existing convention in this file —
useNavigate'sactiveRefeffect (a few lines up) is already an unguardeduseLayoutEffect, as are several others acrosscomponents.tsxanddom/lib.tsx.Testing
Added a regression test in
use-blocker-test.tsxthat derivesshouldBlockfrom the current route (location.pathname === "/"), matching the issue's real-world shape (a form page that blocks while active, stops blocking once left) rather than a hardcodedtrue. TDD red/green confirmed: reverting just thehooks.tsxchange makes the new test fail withExpected: "unblocked", Received: "blocked".Full
packages/react-routertest suite: 2404/2419 passing. The 2 failing suites (rsc/server-test.ts,dom/dom-export-test.tsx) fail identically on unmodifiedmain— both are pre-existing "Cannot find module" errors requiring apnpm buildstep first, unrelated to this change (confirmed viagit stash).packages/react-router's owntsc --noEmitpasses clean.eslinton the changed files is clean.Added a change file per
docs/community/contributing.md.