-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(router-core): keep useLocation on the rendered route's location #8038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| '@tanstack/router-core': patch | ||
| '@tanstack/react-router': patch | ||
| '@tanstack/solid-router': patch | ||
| '@tanstack/vue-router': patch | ||
| --- | ||
|
|
||
| `useLocation` now returns the location that produced the matches currently | ||
| being rendered instead of the location the router has parsed but not yet | ||
| loaded. A component on the route being left no longer re-renders with the | ||
| destination's pathname before it unmounts. The presented location is | ||
| published on the same lane as the matches it describes, so the two can never | ||
| disagree. `router.state.location` is unchanged and still reports the | ||
| requested location while it loads. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { act } from 'react' | ||
| import { cleanup, render, screen } from '@testing-library/react' | ||
| import { afterEach, expect, test } from 'vitest' | ||
| import { createMemoryHistory } from '@tanstack/history' | ||
| import { | ||
| Outlet, | ||
| RouterProvider, | ||
| createRootRoute, | ||
| createRoute, | ||
| createRouter, | ||
| useLocation, | ||
| } from '../src' | ||
|
|
||
| afterEach(() => { | ||
| cleanup() | ||
| }) | ||
|
|
||
| // https://github.com/TanStack/router/issues/8037 | ||
| test('#8037: useLocation in a route component does not report the pathname it is navigating to', async () => { | ||
| const seen: Array<string> = [] | ||
|
|
||
| function Probe() { | ||
| seen.push(useLocation().pathname) | ||
| return <div>Posts</div> | ||
| } | ||
|
|
||
| const rootRoute = createRootRoute({ component: () => <Outlet /> }) | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: () => <div>Home</div>, | ||
| }) | ||
| const postsRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/posts', | ||
| component: Probe, | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute, postsRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/posts'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
| expect(await screen.findByText('Posts')).toBeInTheDocument() | ||
|
|
||
| await act(() => router.navigate({ to: '/' })) | ||
| expect(await screen.findByText('Home')).toBeInTheDocument() | ||
|
|
||
| expect(seen).not.toContain('/') | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,15 +28,15 @@ export function useLocation< | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const router = useRouter<TRouter>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!opts?.select) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (() => router.stores.location.get()) as Accessor< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (() => router.stores.presentedLocation.get()) as Accessor< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UseLocationResult<TRouter, TSelected> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const select = opts.select | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Solid.createMemo((prev: TSelected | undefined) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const res = select(router.stores.location.get()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const res = select(router.stores.presentedLocation.get()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (prev === undefined) return res | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return replaceEqualDeep(prev, res) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
30
to
41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Add braces to both Line 30 and line 40 use unbraced control bodies. Add braces while changing this function. Proposed fix- if (!opts?.select) {
+ if (!opts?.select) {
return (() => router.stores.presentedLocation.get()) as Accessor<
UseLocationResult<TRouter, TSelected>
>
}
@@
- if (prev === undefined) return res
+ if (prev === undefined) {
+ return res
+ }As per coding guidelines, 📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSources: Coding guidelines, Learnings |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) as Accessor<UseLocationResult<TRouter, TSelected>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: TanStack/router
Length of output: 50372
🏁 Script executed:
Repository: TanStack/router
Length of output: 18632
🏁 Script executed:
Repository: TanStack/router
Length of output: 12429
🏁 Script executed:
Repository: TanStack/router
Length of output: 17099
🏁 Script executed:
Repository: TanStack/router
Length of output: 751
🏁 Script executed:
Repository: TanStack/router
Length of output: 1346
Preserve the route search schema in
_committedLocation.ParsedLocationdefaults itssearchtype to{}, which removes route-specific search keys and value types. UseParsedLocation<FullSearchSchema<TRouteTree>>, consistent withlatestLocation.🤖 Prompt for AI Agents
Source: Coding guidelines