@@ -23,6 +23,7 @@ import { useLayoutEffect } from './utils'
2323import type {
2424 AnyRoute ,
2525 AnyRouteMatch ,
26+ ParsedLocation ,
2627 RootRouteOptions ,
2728} from '@tanstack/router-core'
2829
@@ -247,7 +248,15 @@ function OnRendered() {
247248 }
248249
249250 // eslint-disable-next-line react-hooks/rules-of-hooks
250- const prevHrefRef = React . useRef < string | undefined > ( undefined )
251+ // @ts -expect-error -- init to `undefined` but don't write `undefined` to shave bytes
252+ // Track the resolvedLocation as of the last render so that onRendered can
253+ // report the correct fromLocation. By the time this effect fires,
254+ // resolvedLocation has already been updated to the new location by
255+ // Transitioner, so we cannot use router.stores.resolvedLocation.get()
256+ // directly as the fromLocation.
257+ const prevResolvedLocationRef = React . useRef <
258+ ParsedLocation < any > | undefined
259+ > ( )
251260 // eslint-disable-next-line react-hooks/rules-of-hooks
252261 const renderedLocationKey = useStore (
253262 router . stores . resolvedLocation ,
@@ -256,21 +265,23 @@ function OnRendered() {
256265
257266 // eslint-disable-next-line react-hooks/rules-of-hooks
258267 useLayoutEffect ( ( ) => {
259- const currentHref = router . latestLocation . href
268+ const currentResolvedLocation = router . stores . resolvedLocation . get ( )
269+ const previousResolvedLocation = prevResolvedLocationRef . current
260270
261271 if (
262- prevHrefRef . current === undefined ||
263- prevHrefRef . current !== currentHref
272+ currentResolvedLocation &&
273+ ( ! previousResolvedLocation ||
274+ previousResolvedLocation . href !== currentResolvedLocation . href )
264275 ) {
265276 router . emit ( {
266277 type : 'onRendered' ,
267278 ...getLocationChangeInfo (
268279 router . stores . location . get ( ) ,
269- router . stores . resolvedLocation . get ( ) ,
280+ previousResolvedLocation ?? currentResolvedLocation ,
270281 ) ,
271282 } )
272- prevHrefRef . current = currentHref
273283 }
284+ prevResolvedLocationRef . current = currentResolvedLocation
274285 } , [ renderedLocationKey , router ] )
275286
276287 return null
0 commit comments