Skip to content
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

fix(react-router): parentMatchPromiseresolves to parent match #2666

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion docs/framework/react/guide/data-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ The `loader` function receives a single object with the following properties:
- `deps` - The object value returned from the `Route.loaderDeps` function. If `Route.loaderDeps` is not defined, an empty object will be provided instead.
- `location` - The current location
- `params` - The route's path params
- `parentMatchPromise` - `Promise<void>` or `undefined`
- `parentMatchPromise` - `Promise<RouteMatch>` or `undefined`
- `preload` - Boolean which is `true` when the route is being preloaded instead of loaded
- `route` - The route itself

Expand Down
10 changes: 10 additions & 0 deletions packages/react-router/src/Matches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ export const isMatch = <TMatch, TPath extends string>(
return value != null
}

export type MakeRouteMatchFromRoute<TRoute extends AnyRoute> = RouteMatch<
TRoute['types']['id'],
TRoute['types']['fullPath'],
TRoute['types']['allParams'],
TRoute['types']['fullSearchSchema'],
TRoute['types']['loaderData'],
TRoute['types']['allContext'],
TRoute['types']['loaderDeps']
>

export interface RouteMatch<
TRouteId,
TFullPath,
Expand Down
9 changes: 7 additions & 2 deletions packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import { rootRouteId } from './root'
import type * as React from 'react'
import type { RootRouteId } from './root'
import type { UseNavigateResult } from './useNavigate'
import type { MakeRouteMatch, MakeRouteMatchUnion, RouteMatch } from './Matches'
import type {
MakeRouteMatch,
MakeRouteMatchFromRoute,
MakeRouteMatchUnion,
RouteMatch,
} from './Matches'
import type { NavigateOptions, ParsePathParams, ToMaskOptions } from './link'
import type { ParsedLocation } from './location'
import type { RouteById, RouteIds, RoutePaths } from './routeInfo'
Expand Down Expand Up @@ -591,7 +596,7 @@ export interface LoaderFnContext<
* @deprecated Use `throw redirect({ to: '/somewhere' })` instead
**/
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatchFromRoute<TParentRoute>>
cause: 'preload' | 'enter' | 'stay'
route: Route
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,7 @@ export class Router<
}

const validResolvedMatches = matches.slice(0, firstBadMatchIndex)
const matchPromises: Array<Promise<any>> = []
const matchPromises: Array<Promise<AnyRouteMatch>> = []

validResolvedMatches.forEach(({ id: matchId, routeId }, index) => {
matchPromises.push(
Expand Down Expand Up @@ -2535,6 +2535,7 @@ export class Router<
loaderPromise: undefined,
invalid: false,
}))
return this.getMatch(matchId)!
})(),
)
})
Expand Down
5 changes: 4 additions & 1 deletion packages/react-router/tests/loaders.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,17 @@ describe('loaders parentMatchPromise', () => {
path: '/nested',
loader: async () => {
await sleep(WAIT_TIME)
return 'nested'
},
component: () => <Outlet />,
})
const fooRoute = createRoute({
getParentRoute: () => nestedRoute,
path: '/foo',
loader: ({ parentMatchPromise }) => {
loader: async ({ parentMatchPromise }) => {
nestedLoaderMock(parentMatchPromise)
const parentMatch = await parentMatchPromise
expect(parentMatch?.loaderData).toBe('nested')
},
component: () => <div>Nested Foo page</div>,
})
Expand Down
28 changes: 17 additions & 11 deletions packages/react-router/tests/route.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import type {
Route,
SearchSchemaInput,
} from '../src'
import type { MakeRouteMatchUnion } from '../src/Matches'
import type {
MakeRouteMatch,
MakeRouteMatchFromRoute,
MakeRouteMatchUnion,
} from '../src/Matches'

test('when creating the root', () => {
const rootRoute = createRootRoute()
Expand Down Expand Up @@ -83,7 +87,7 @@ test('when creating the root with a loader', () => {
context: {}
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatch>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>()
Expand Down Expand Up @@ -178,7 +182,7 @@ test('when creating the root route with context and a loader', () => {
context: { userId: string }
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatch>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>()
Expand Down Expand Up @@ -246,7 +250,7 @@ test('when creating the root route with context, routeContext, beforeLoad and a
context: { userId: string; permission: 'view'; env: 'env1' }
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatch>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>()
Expand Down Expand Up @@ -374,7 +378,7 @@ test('when creating a child route with a loader from the root route', () => {
context: {}
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatchFromRoute<typeof rootRoute>>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>()
Expand Down Expand Up @@ -416,7 +420,7 @@ test('when creating a child route with a loader from the root route with context
context: { userId: string }
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatchFromRoute<typeof rootRoute>>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>()
Expand Down Expand Up @@ -549,7 +553,7 @@ test('when creating a child route with params, search and loader from the root r
context: {}
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatchFromRoute<typeof rootRoute>>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>
Expand All @@ -574,7 +578,7 @@ test('when creating a child route with params, search, loader and loaderDeps fro
context: {}
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatchFromRoute<typeof rootRoute>>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>(),
Expand All @@ -598,7 +602,7 @@ test('when creating a child route with params, search, loader and loaderDeps fro
context: { userId: string }
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatchFromRoute<typeof rootRoute>>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>(),
Expand Down Expand Up @@ -701,7 +705,7 @@ test('when creating a child route with params, search with routeContext, beforeL
context: { userId: string; env: string; readonly permission: 'view' }
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<MakeRouteMatchFromRoute<typeof rootRoute>>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>()
Expand Down Expand Up @@ -1030,7 +1034,9 @@ test('when creating a child route with routeContext, beforeLoad, search, params,
}
location: ParsedLocation
navigate: (opts: NavigateOptions<AnyRouter>) => Promise<void>
parentMatchPromise?: Promise<void>
parentMatchPromise?: Promise<
MakeRouteMatchFromRoute<typeof detailsRoute>
>
cause: 'preload' | 'enter' | 'stay'
route: Route
}>(),
Expand Down