@@ -21,30 +21,66 @@ import type {
21
21
import { Outlet , RouterProvider , createMemoryRouter } from "../../components" ;
22
22
import type { EntryRoute } from "./routes" ;
23
23
import { FrameworkContext } from "./components" ;
24
+ import {
25
+ useParams ,
26
+ useLoaderData ,
27
+ useActionData ,
28
+ useMatches ,
29
+ useRouteError ,
30
+ } from "../../hooks" ;
24
31
25
- interface StubIndexRouteObject
26
- extends Omit <
27
- IndexRouteObject ,
28
- "loader" | "action" | "element" | "errorElement" | "children"
29
- > {
32
+ interface StubRouteExtensions {
33
+ Component ?: React . ComponentType < {
34
+ params : ReturnType < typeof useParams > ;
35
+ loaderData : ReturnType < typeof useLoaderData > ;
36
+ actionData : ReturnType < typeof useActionData > ;
37
+ matches : ReturnType < typeof useMatches > ;
38
+ } > ;
39
+ HydrateFallback ?: React . ComponentType < {
40
+ params : ReturnType < typeof useParams > ;
41
+ loaderData : ReturnType < typeof useLoaderData > ;
42
+ actionData : ReturnType < typeof useActionData > ;
43
+ } > ;
44
+ ErrorBoundary ?: React . ComponentType < {
45
+ params : ReturnType < typeof useParams > ;
46
+ loaderData : ReturnType < typeof useLoaderData > ;
47
+ actionData : ReturnType < typeof useActionData > ;
48
+ error : ReturnType < typeof useRouteError > ;
49
+ } > ;
30
50
loader ?: LoaderFunction ;
31
51
action ?: ActionFunction ;
32
52
children ?: StubRouteObject [ ] ;
33
53
meta ?: MetaFunction ;
34
54
links ?: LinksFunction ;
35
55
}
36
56
57
+ interface StubIndexRouteObject
58
+ extends Omit <
59
+ IndexRouteObject ,
60
+ | "Component"
61
+ | "HydrateFallback"
62
+ | "ErrorBoundary"
63
+ | "loader"
64
+ | "action"
65
+ | "element"
66
+ | "errorElement"
67
+ | "children"
68
+ > ,
69
+ StubRouteExtensions { }
70
+
37
71
interface StubNonIndexRouteObject
38
72
extends Omit <
39
- NonIndexRouteObject ,
40
- "loader" | "action" | "element" | "errorElement" | "children"
41
- > {
42
- loader ?: LoaderFunction ;
43
- action ?: ActionFunction ;
44
- children ?: StubRouteObject [ ] ;
45
- meta ?: MetaFunction ;
46
- links ?: LinksFunction ;
47
- }
73
+ NonIndexRouteObject ,
74
+ | "Component"
75
+ | "HydrateFallback"
76
+ | "ErrorBoundary"
77
+ | "loader"
78
+ | "action"
79
+ | "element"
80
+ | "errorElement"
81
+ | "children"
82
+ > ,
83
+ StubRouteExtensions { }
48
84
49
85
type StubRouteObject = StubIndexRouteObject | StubNonIndexRouteObject ;
50
86
@@ -141,6 +177,41 @@ export function createRoutesStub(
141
177
} ;
142
178
}
143
179
180
+ // Implementations copied from packages/react-router-dev/vite/with-props.ts
181
+ function withComponentProps ( Component : React . ComponentType < any > ) {
182
+ return function Wrapped ( ) {
183
+ return React . createElement ( Component , {
184
+ params : useParams ( ) ,
185
+ loaderData : useLoaderData ( ) ,
186
+ actionData : useActionData ( ) ,
187
+ matches : useMatches ( ) ,
188
+ } ) ;
189
+ } ;
190
+ }
191
+
192
+ function withHydrateFallbackProps ( HydrateFallback : React . ComponentType < any > ) {
193
+ return function Wrapped ( ) {
194
+ const props = {
195
+ params : useParams ( ) ,
196
+ loaderData : useLoaderData ( ) ,
197
+ actionData : useActionData ( ) ,
198
+ } ;
199
+ return React . createElement ( HydrateFallback , props ) ;
200
+ } ;
201
+ }
202
+
203
+ function withErrorBoundaryProps ( ErrorBoundary : React . ComponentType < any > ) {
204
+ return function Wrapped ( ) {
205
+ const props = {
206
+ params : useParams ( ) ,
207
+ loaderData : useLoaderData ( ) ,
208
+ actionData : useActionData ( ) ,
209
+ error : useRouteError ( ) ,
210
+ } ;
211
+ return React . createElement ( ErrorBoundary , props ) ;
212
+ } ;
213
+ }
214
+
144
215
function processRoutes (
145
216
routes : StubRouteObject [ ] ,
146
217
manifest : AssetsManifest ,
@@ -158,9 +229,15 @@ function processRoutes(
158
229
id : route . id ,
159
230
path : route . path ,
160
231
index : route . index ,
161
- Component : route . Component ,
162
- HydrateFallback : route . HydrateFallback ,
163
- ErrorBoundary : route . ErrorBoundary ,
232
+ Component : route . Component
233
+ ? withComponentProps ( route . Component )
234
+ : undefined ,
235
+ HydrateFallback : route . HydrateFallback
236
+ ? withHydrateFallbackProps ( route . HydrateFallback )
237
+ : undefined ,
238
+ ErrorBoundary : route . ErrorBoundary
239
+ ? withErrorBoundaryProps ( route . ErrorBoundary )
240
+ : undefined ,
164
241
action : route . action ,
165
242
loader : route . loader ,
166
243
handle : route . handle ,
@@ -193,8 +270,8 @@ function processRoutes(
193
270
194
271
// Add the route to routeModules
195
272
routeModules [ route . id ] = {
196
- default : route . Component || Outlet ,
197
- ErrorBoundary : route . ErrorBoundary || undefined ,
273
+ default : newRoute . Component || Outlet ,
274
+ ErrorBoundary : newRoute . ErrorBoundary || undefined ,
198
275
handle : route . handle ,
199
276
links : route . links ,
200
277
meta : route . meta ,
0 commit comments