Repository navigation
[router] Fix getState() returning stale state in state listeners - #51194
Merged
Merged
Conversation
Contributor
|
Subscribed to pull request
Generated by CodeMention |
# Why Fixes expo#51188. Inside a `state` event listener, `navigation.getState()` returned the previous state while `event.data.state` held the new one. # How The `state` emit effect was declared before `useNavigationHelpers`. The helpers' `getState` is backed by `useLatestCallback`, which refreshes its ref in `useInsertionEffect` only when `document` is defined or `navigator.product` is `ReactNative`, and otherwise in a passive effect (the case in Jest). Effects run in declaration order, so listeners ran before the ref was refreshed. The emit effect now follows `useNavigationHelpers`. # Test Plan Added `state-listener.test.ios.tsx`, which compares `getState()` with `event.data.state` in a `state` listener after two navigations. It fails without the change and passes with it. `pnpm exec jest src/__tests__/state-listener` in `packages/expo-router`.
Co-authored-by: ubax <[email protected]>
jakub-agent
force-pushed
the
claude/51188-fix
branch
from
October 12, 2026 13:37
a0cbcd8 to
18496ac
Compare
Co-authored-by: ubax <[email protected]>
Ubax
approved these changes
Oct 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Fixes #51188. Inside a
stateevent listener,navigation.getState()returned the previous state whileevent.data.stateheld the new one.How
The
stateemit effect ran beforeuseNavigationHelpers, whosegetStateref is refreshed in a passive effect when not on web or React Native (e.g. Jest). Effects run in declaration order, so listeners saw the old ref. The emit effect now followsuseNavigationHelpers.Test Plan
Added
state-listener.test.ios.tsx, which comparesgetState()withevent.data.statein astatelistener after two navigations. It fails before the change and passes after (pnpm exec jest src/__tests__/state-listenerinpackages/expo-router).Checklist
npx expo prebuild& EAS Build (eg: updated a module plugin). Not applicable, no plugin change.