Skip to content

feat: provide location in onEnter/onStay/onLeave hooks #3622

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/router/framework/react/api/router/RouteOptionsType.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,19 @@ type loaderDeps = (opts: { search: TFullSearchSchema }) => Record<string, any>

### `onEnter` property

- Type: `(match: RouteMatch) => void`
- Type: `(match: RouteMatch, { location }: { location: ParsedLocation<{}> }) => void`
- Optional
- A function that will be called when a route is matched and loaded after not being matched in the previous location.

### `onStay` property

- Type: `(match: RouteMatch) => void`
- Type: `(match: RouteMatch, { location }: { location: ParsedLocation<{}> }) => void`
- Optional
- A function that will be called when a route is matched and loaded after being matched in the previous location.

### `onLeave` property

- Type: `(match: RouteMatch) => void`
- Type: `(match: RouteMatch, { location }: { location: ParsedLocation<{}> }) => void`
- Optional
- A function that will be called when a route is no longer matched after being matched in the previous location.

Expand Down
5 changes: 4 additions & 1 deletion packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,10 @@ export class Router<
] as const
).forEach(([matches, hook]) => {
matches.forEach((match) => {
this.looseRoutesById[match.routeId]!.options[hook]?.(match)
this.looseRoutesById[match.routeId]!.options[hook]?.(
match,
{ location: next },
)
})
})
})
Expand Down
15 changes: 12 additions & 3 deletions packages/react-router/tests/route.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1321,9 +1321,18 @@ test('when creating a child route with context, search, params, loader, loaderDe
invoicePage: deps.search.page,
}),
loader: () => ({ detailLoader: 'detailResult' }) as const,
onEnter: (match) => expectTypeOf(match).toMatchTypeOf<TExpectedMatch>(),
onStay: (match) => expectTypeOf(match).toMatchTypeOf<TExpectedMatch>(),
onLeave: (match) => expectTypeOf(match).toMatchTypeOf<TExpectedMatch>(),
onEnter: (match, { location }) => {
expectTypeOf(match).toMatchTypeOf<TExpectedMatch>()
expectTypeOf(location).toMatchTypeOf<ParsedLocation<{}>>()
},
onStay: (match, { location }) => {
expectTypeOf(match).toMatchTypeOf<TExpectedMatch>()
expectTypeOf(location).toMatchTypeOf<ParsedLocation<{}>>()
},
onLeave: (match, { location }) => {
expectTypeOf(match).toMatchTypeOf<TExpectedMatch>()
expectTypeOf(location).toMatchTypeOf<ParsedLocation<{}>>()
},
})
})

Expand Down
21 changes: 21 additions & 0 deletions packages/react-router/tests/route.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,27 @@ describe('onEnter event', () => {

expect(fn).toHaveBeenCalledWith({ foo: 'bar' })
})

it('should have location defined in router.load()', async () => {
const fn = vi.fn()
const rootRoute = createRootRoute()
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return <h1>Index</h1>
},
onEnter: (_, { location: { href, pathname } }) => {
fn({ href, pathname })
},
})
const routeTree = rootRoute.addChildren([indexRoute])
const router = createRouter({ routeTree })

await router.load()

expect(fn).toHaveBeenCalledWith({ href: '/', pathname: '/' })
})
})

describe('route.head', () => {
Expand Down
22 changes: 21 additions & 1 deletion packages/router-core/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import type {
RouteMatch,
} from './Matches'
import type { RootRouteId } from './root'
import type { ParseRoute, RouteById, RoutePaths } from './routeInfo'
import type {
FullSearchSchema,
ParseRoute,
RouteById,
RoutePaths,
} from './routeInfo'
import type { AnyRouter, RegisteredRouter } from './router'
import type { BuildLocationFn, NavigateFn } from './RouterProvider'
import type {
Expand Down Expand Up @@ -1032,6 +1037,13 @@ export interface UpdatableRouteOptions<
>,
TLoaderDeps
>,
{
location,
}: {
location: ParsedLocation<
ResolveFullSearchSchema<TParentRoute, TSearchValidator>
>
},
) => void
onStay?: (
match: RouteMatch<
Expand All @@ -1048,6 +1060,13 @@ export interface UpdatableRouteOptions<
>,
TLoaderDeps
>,
{
location,
}: {
location: ParsedLocation<
ResolveFullSearchSchema<TParentRoute, TSearchValidator>
>
},
) => void
onLeave?: (
match: RouteMatch<
Expand All @@ -1064,6 +1083,7 @@ export interface UpdatableRouteOptions<
>,
TLoaderDeps
>,
{ location }: { location: ParsedLocation<FullSearchSchema<TRouteTree>> },
) => void
headers?: (ctx: {
loaderData: ResolveLoaderData<TLoaderFn>
Expand Down
5 changes: 4 additions & 1 deletion packages/solid-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,10 @@ export class Router<
] as const
).forEach(([matches, hook]) => {
matches.forEach((match) => {
this.looseRoutesById[match.routeId]!.options[hook]?.(match)
this.looseRoutesById[match.routeId]!.options[hook]?.(
match,
{ location: next },
)
})
})
})
Expand Down
15 changes: 12 additions & 3 deletions packages/solid-router/tests/route.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1286,9 +1286,18 @@ test('when creating a child route with context, search, params, loader, loaderDe
invoicePage: deps.search.page,
}),
loader: () => ({ detailLoader: 'detailResult' }) as const,
onEnter: (match) => expectTypeOf(match).toMatchTypeOf<TExpectedMatch>(),
onStay: (match) => expectTypeOf(match).toMatchTypeOf<TExpectedMatch>(),
onLeave: (match) => expectTypeOf(match).toMatchTypeOf<TExpectedMatch>(),
onEnter: (match, { location }) => {
expectTypeOf(match).toMatchTypeOf<TExpectedMatch>()
expectTypeOf(location).toMatchTypeOf<ParsedLocation<{}>>()
},
onStay: (match, { location }) => {
expectTypeOf(match).toMatchTypeOf<TExpectedMatch>()
expectTypeOf(location).toMatchTypeOf<ParsedLocation<{}>>()
},
onLeave: (match, { location }) => {
expectTypeOf(match).toMatchTypeOf<TExpectedMatch>()
expectTypeOf(location).toMatchTypeOf<ParsedLocation<{}>>()
},
})
})

Expand Down
21 changes: 21 additions & 0 deletions packages/solid-router/tests/route.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ describe('onEnter event', () => {

expect(fn).toHaveBeenCalledWith({ foo: 'bar' })
})

it('should have location defined in router.load()', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather should have location defined in onEnter?

please also add a test for onStay and onLeave

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also applies to react-router of course

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was just copying the style from above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its in an onEnter describe block

const fn = vi.fn()
const rootRoute = createRootRoute()
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return <h1>Index</h1>
},
onEnter: (_, { location: { href, pathname } }) => {
fn({ href, pathname })
},
})
const routeTree = rootRoute.addChildren([indexRoute])
const router = createRouter({ routeTree })

await router.load()

expect(fn).toHaveBeenCalledWith({ href: '/', pathname: '/' })
})
})

describe('route.head', () => {
Expand Down
Loading