Skip to content

Commit 0ae150e

Browse files
committed
reactivity and code cleanup
1 parent f5c5559 commit 0ae150e

File tree

4 files changed

+37
-45
lines changed

4 files changed

+37
-45
lines changed

packages/react-router/src/link.tsx

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
deepEqual,
55
exactPathTest,
66
functionalUpdate,
7+
last,
78
preloadWarning,
89
removeTrailingSlash,
910
} from '@tanstack/router-core'
@@ -104,24 +105,28 @@ export function useLinkProps<
104105
select: (match) => match.index,
105106
})
106107

107-
const getFrom = React.useCallback(() => {
108-
const currentRouteMatches = router.matchRoutes(router.latestLocation, {
109-
_buildLocation: false,
110-
})
108+
// Track the active location to ensure recomputation on path changes
109+
const activeLocation = useRouterState({
110+
select: (s) => s.location,
111+
structuralSharing: true as any,
112+
})
111113

112-
return (
113-
options.from ??
114-
currentRouteMatches.slice(-1)[0]?.fullPath ??
115-
router.state.matches[matchIndex]!.fullPath
116-
)
117-
}, [router, options.from, matchIndex])
114+
const _options = React.useMemo(() => {
115+
const currentRouteMatches = router.matchRoutes(activeLocation, {
116+
_buildLocation: false,
117+
})
118118

119-
const next = React.useMemo(
120-
() => router.buildLocation({ ...options, from: getFrom() } as any),
119+
const from = options.from ??
120+
last(currentRouteMatches)?.fullPath ??
121+
router.state.matches[matchIndex]!.fullPath
122+
123+
return {...options, from}
124+
},
121125
// eslint-disable-next-line react-hooks/exhaustive-deps
122126
[
123127
router,
124128
currentSearch,
129+
activeLocation,
125130
options._fromLocation,
126131
options.from,
127132
options.hash,
@@ -131,8 +136,12 @@ export function useLinkProps<
131136
options.state,
132137
options.mask,
133138
options.unsafeRelative,
134-
getFrom,
135-
],
139+
]
140+
)
141+
142+
const next = React.useMemo(
143+
() => router.buildLocation({ ..._options } as any),
144+
[router, _options]
136145
)
137146

138147
const isExternal = type === 'external'
@@ -196,33 +205,13 @@ export function useLinkProps<
196205
const doPreload = React.useCallback(
197206
() => {
198207
router
199-
.preloadRoute({ ...options, from: getFrom() } as any)
208+
.preloadRoute({ ... _options } as any)
200209
.catch((err) => {
201210
console.warn(err)
202211
console.warn(preloadWarning)
203212
})
204213
},
205-
// eslint-disable-next-line react-hooks/exhaustive-deps
206-
[
207-
router,
208-
options.to,
209-
options._fromLocation,
210-
options.from,
211-
options.search,
212-
options.hash,
213-
options.params,
214-
options.state,
215-
options.mask,
216-
options.unsafeRelative,
217-
options.hashScrollIntoView,
218-
options.href,
219-
options.ignoreBlocker,
220-
options.reloadDocument,
221-
options.replace,
222-
options.resetScroll,
223-
options.viewTransition,
224-
getFrom,
225-
],
214+
[router, _options]
226215
)
227216

228217
const preloadViewportIoCallback = React.useCallback(
@@ -274,8 +263,7 @@ export function useLinkProps<
274263
// All is well? Navigate!
275264
// N.B. we don't call `router.commitLocation(next) here because we want to run `validateSearch` before committing
276265
router.navigate({
277-
...options,
278-
from: getFrom(),
266+
..._options,
279267
replace,
280268
resetScroll,
281269
hashScrollIntoView,

packages/react-router/src/useNavigate.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react'
2+
import { last } from '@tanstack/router-core'
23
import { useRouter } from './useRouter'
34
import { useMatch } from './useMatch'
45
import type {
@@ -33,7 +34,7 @@ export function useNavigate<
3334
const from =
3435
options.from ??
3536
_defaultOpts?.from ??
36-
currentRouteMatches.slice(-1)[0]?.fullPath ??
37+
last(currentRouteMatches)?.fullPath ??
3738
router.state.matches[matchIndex]!.fullPath
3839

3940
return router.navigate({

packages/solid-router/src/link.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
deepEqual,
77
exactPathTest,
88
functionalUpdate,
9+
last,
910
preloadWarning,
1011
removeTrailingSlash,
1112
} from '@tanstack/router-core'
@@ -147,19 +148,20 @@ export function useLinkProps<
147148

148149
const activeLocation = useRouterState({ select: (s) => s.location })
149150

150-
const _options = () => {
151+
const from = Solid.createMemo(() => {
151152
const currentRouteMatches = router.matchRoutes(activeLocation(), {
152153
_buildLocation: false,
153154
})
154155

155-
const from =
156-
options.from ??
157-
currentRouteMatches.slice(-1)[0]?.fullPath ??
156+
return options.from ??
157+
last(currentRouteMatches)?.fullPath ??
158158
router.state.matches[matchIndex()]!.fullPath
159+
})
159160

161+
const _options = () => {
160162
return {
161163
...options,
162-
from,
164+
from: from(),
163165
}
164166
}
165167

packages/solid-router/src/useNavigate.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Solid from 'solid-js'
2+
import { last } from '@tanstack/router-core'
23
import { useRouter } from './useRouter'
34
import { useMatch } from './useMatch'
45
import type {
@@ -32,7 +33,7 @@ export function useNavigate<
3233
from:
3334
options.from ??
3435
_defaultOpts?.from ??
35-
currentRouteMatches.slice(-1)[0]?.fullPath ??
36+
last(currentRouteMatches)?.fullPath ??
3637
router.state.matches[matchIndex()]!.fullPath,
3738
})
3839
}) as UseNavigateResult<TDefaultFrom>

0 commit comments

Comments
 (0)