Skip to content

Commit

Permalink
fix: Revert "fix: from was being ignored when the pathname included…
Browse files Browse the repository at this point in the history
… params or a pathless route (#1703)" (#1716)

This reverts commit 56a3100.

# Conflicts:
#	packages/react-router/tests/link.test.tsx
  • Loading branch information
TkDodo committed Jun 6, 2024
1 parent 3bd1cdf commit e82984b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 1,590 deletions.
1 change: 1 addition & 0 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ export function useLinkProps<
// null for LinkUtils
const dest = {
...(options.to && { from: matchPathname }),
...options,
}
Expand Down
23 changes: 6 additions & 17 deletions packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,27 +1102,14 @@ export class Router<
): ParsedLocation => {
let fromPath = this.latestLocation.pathname
let fromSearch = dest.fromSearch || this.latestLocation.search
const looseRoutesByPath = this.routesByPath as Record<string, AnyRoute>

const fromRoute =
dest.from !== undefined
? looseRoutesByPath[trimPathRight(dest.from)]
: undefined

const fromMatches = this.matchRoutes(
this.latestLocation.pathname,
fromSearch,
)

const fromMatch = fromMatches.find((d) => d.routeId === fromRoute?.id)

fromPath = fromMatch?.pathname || fromPath

invariant(
dest.from == null || fromMatch != null,
'Could not find match for from: ' + dest.from,
)

fromPath =
fromMatches.find((d) => d.id === dest.from)?.pathname || fromPath
fromSearch = last(fromMatches)?.search || this.latestLocation.search

const stayingMatches = matches?.filter((d) =>
Expand Down Expand Up @@ -1786,7 +1773,8 @@ export class Router<
preload: !!preload,
context: parentContext,
location,
navigate: (opts: any) => this.navigate({ ...opts }),
navigate: (opts: any) =>
this.navigate({ ...opts, from: match.pathname }),
buildLocation: this.buildLocation,
cause: preload ? 'preload' : match.cause,
})) ?? ({} as any)
Expand Down Expand Up @@ -1839,7 +1827,8 @@ export class Router<
abortController: match.abortController,
context: match.context,
location,
navigate: (opts) => this.navigate({ ...opts } as any),
navigate: (opts) =>
this.navigate({ ...opts, from: match.pathname } as any),
cause: preload ? 'preload' : match.cause,
route,
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-router/src/useNavigate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function useNavigate<
(options: NavigateOptions) => {
return router.navigate({
...options,
from: options.to ? router.state.resolvedLocation.pathname : undefined,
})
},
[router],
Expand Down Expand Up @@ -62,6 +63,7 @@ export function Navigate<

React.useEffect(() => {
navigate({
from: props.to ? match.pathname : undefined,
...props,
} as any)
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
Loading

0 comments on commit e82984b

Please sign in to comment.