Skip to content

fix(react-router): don't block a second time when the destination route updates search params on mount - #15538

Merged
brophdawg11 merged 4 commits into
remix-run:mainfrom
cipherprofessor:fix/useblocker-double-block-on-mount-search-params
Nov 20, 2026
Merged

brophdawg11 merged 4 commits into
remix-run:mainfrom
cipherprofessor:fix/useblocker-double-block-on-mount-search-params

Conversation

@cipherprofessor

Copy link
Copy Markdown
Contributor

Problem

Fixes #11144.

When a blocked navigation is confirmed with blocker.proceed(), and the destination route calls setSearchParams in its own mount effect, useBlocker fires a second, incorrect block — even though the blocking condition is no longer true.

Root cause

useBlocker registers its current blocker-predicate function with the router in a React.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:

  • the blocking component's predicate changes (e.g. it derives shouldBlock from the current route, which just changed), and
  • a newly-mounted child (the destination route) immediately triggers another navigation from its own mount effect (setSearchParams)

...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:

[DEBUG shouldBlockNavigation] currentLocation=/about nextLocation=/about?foo=bar blocker.state=unblocked
[DEBUG shouldBlockNavigation] blockResult= true       <-- stale predicate still says "block"
[DEBUG About] mount effect firing setSearchParams
[DEBUG useBlocker] registering blockerFunction ... shouldBlock=false   <-- updated too late

Fix

Change the blocker-registration effect in useBlocker from useEffect to useLayoutEffect. 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's setSearchParams call) can trigger a navigation check.

This matches existing convention in this file — useNavigate's activeRef effect (a few lines up) is already an unguarded useLayoutEffect, as are several others across components.tsx and dom/lib.tsx.

Testing

Added a regression test in use-blocker-test.tsx that derives shouldBlock from 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 hardcoded true. TDD red/green confirmed: reverting just the hooks.tsx change makes the new test fail with Expected: "unblocked", Received: "blocked".

Full packages/react-router test suite: 2404/2419 passing. The 2 failing suites (rsc/server-test.ts, dom/dom-export-test.tsx) fail identically on unmodified main — both are pre-existing "Cannot find module" errors requiring a pnpm build step first, unrelated to this change (confirmed via git stash).

packages/react-router's own tsc --noEmit passes clean. eslint on the changed files is clean.

Added a change file per docs/community/contributing.md.

…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.
@cipherprofessor
cipherprofessor force-pushed the fix/useblocker-double-block-on-mount-search-params branch from 2d2213c to 5b34082 Compare November 10, 2026 18:12
Cover StrictMode, router recreation, Activity, and server rendering.
Tighten the effect timing comment and patch change note.
@brophdawg11

Copy link
Copy Markdown
Contributor

Thanks for the fix! I added a follow-up in 096698881 that keeps the layout-effect registration and router ownership guard. It expands the regression coverage for a destination route that calls setSearchParams in a mount effect after the user clicks Proceed. The test covers both regular rendering and Strict Mode. The Activity and router-recreation tests also run in both modes, and the SSR test checks that the blocker renders as unblocked without warnings.

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 brophdawg11 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@brophdawg11
brophdawg11 merged commit 6abdb19 into remix-run:main Nov 20, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: useBlocker block twice when navigating on a page using setSearchParams

2 participants