@@ -99,19 +99,29 @@ export function useLinkProps<
99
99
structuralSharing : true as any ,
100
100
} )
101
101
102
- const from = useMatch ( {
102
+ const matchIndex = useMatch ( {
103
103
strict : false ,
104
- select : ( match ) => options . from ?? match . fullPath ,
104
+ select : ( match ) => match . index ,
105
105
} )
106
106
107
+ const getFrom = React . useCallback ( ( ) => {
108
+ const currentRouteMatches = router . matchRoutes ( router . latestLocation , {
109
+ _buildLocation : false ,
110
+ } )
111
+
112
+ return options . from ??
113
+ currentRouteMatches . slice ( - 1 ) [ 0 ] ?. fullPath ??
114
+ router . state . matches [ matchIndex ] ! . fullPath
115
+ } , [ router , options . from , matchIndex ] )
116
+
107
117
const next = React . useMemo (
108
- ( ) => router . buildLocation ( { ...options , from } as any ) ,
118
+ ( ) => router . buildLocation ( { ...options , from : getFrom ( ) } as any ) ,
109
119
// eslint-disable-next-line react-hooks/exhaustive-deps
110
120
[
111
121
router ,
112
122
currentSearch ,
113
123
options . _fromLocation ,
114
- from ,
124
+ options . from ,
115
125
options . hash ,
116
126
options . to ,
117
127
options . search ,
@@ -182,7 +192,7 @@ export function useLinkProps<
182
192
183
193
const doPreload = React . useCallback (
184
194
( ) => {
185
- router . preloadRoute ( { ...options , from } as any ) . catch ( ( err ) => {
195
+ router . preloadRoute ( { ...options , from : getFrom ( ) } as any ) . catch ( ( err ) => {
186
196
console . warn ( err )
187
197
console . warn ( preloadWarning )
188
198
} )
@@ -192,7 +202,7 @@ export function useLinkProps<
192
202
router ,
193
203
options . to ,
194
204
options . _fromLocation ,
195
- from ,
205
+ options . from ,
196
206
options . search ,
197
207
options . hash ,
198
208
options . params ,
@@ -235,24 +245,18 @@ export function useLinkProps<
235
245
}
236
246
} , [ disabled , doPreload , preload ] )
237
247
238
- if ( isExternal ) {
239
- return {
240
- ...propsSafeToSpread ,
241
- ref : innerRef as React . ComponentPropsWithRef < 'a' > [ 'ref' ] ,
242
- type,
243
- href : to ,
244
- ...( children && { children } ) ,
245
- ...( target && { target } ) ,
246
- ...( disabled && { disabled } ) ,
247
- ...( style && { style } ) ,
248
- ...( className && { className } ) ,
249
- ...( onClick && { onClick } ) ,
250
- ...( onFocus && { onFocus } ) ,
251
- ...( onMouseEnter && { onMouseEnter } ) ,
252
- ...( onMouseLeave && { onMouseLeave } ) ,
253
- ...( onTouchStart && { onTouchStart } ) ,
254
- }
255
- }
248
+ const navigate = React . useCallback ( ( ) => {
249
+ router . navigate ( {
250
+ ...options ,
251
+ from : getFrom ( ) ,
252
+ replace,
253
+ resetScroll,
254
+ hashScrollIntoView,
255
+ startTransition,
256
+ viewTransition,
257
+ ignoreBlocker,
258
+ } )
259
+ } , [ router , options , getFrom , replace , resetScroll , hashScrollIntoView , startTransition , viewTransition , ignoreBlocker ] )
256
260
257
261
// The click handler
258
262
const handleClick = ( e : React . MouseEvent ) => {
@@ -276,16 +280,26 @@ export function useLinkProps<
276
280
277
281
// All is well? Navigate!
278
282
// N.B. we don't call `router.commitLocation(next) here because we want to run `validateSearch` before committing
279
- router . navigate ( {
280
- ...options ,
281
- from,
282
- replace,
283
- resetScroll,
284
- hashScrollIntoView,
285
- startTransition,
286
- viewTransition,
287
- ignoreBlocker,
288
- } )
283
+ navigate ( )
284
+ }
285
+ }
286
+
287
+ if ( isExternal ) {
288
+ return {
289
+ ...propsSafeToSpread ,
290
+ ref : innerRef as React . ComponentPropsWithRef < 'a' > [ 'ref' ] ,
291
+ type,
292
+ href : to ,
293
+ ...( children && { children } ) ,
294
+ ...( target && { target } ) ,
295
+ ...( disabled && { disabled } ) ,
296
+ ...( style && { style } ) ,
297
+ ...( className && { className } ) ,
298
+ ...( onClick && { onClick } ) ,
299
+ ...( onFocus && { onFocus } ) ,
300
+ ...( onMouseEnter && { onMouseEnter } ) ,
301
+ ...( onMouseLeave && { onMouseLeave } ) ,
302
+ ...( onTouchStart && { onTouchStart } ) ,
289
303
}
290
304
}
291
305
0 commit comments