@@ -21,30 +21,66 @@ import type {
2121import { Outlet , RouterProvider , createMemoryRouter } from "../../components" ;
2222import type { EntryRoute } from "./routes" ;
2323import { FrameworkContext } from "./components" ;
24+ import {
25+ useParams ,
26+ useLoaderData ,
27+ useActionData ,
28+ useMatches ,
29+ useRouteError ,
30+ } from "../../hooks" ;
2431
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+ } > ;
3050 loader ?: LoaderFunction ;
3151 action ?: ActionFunction ;
3252 children ?: StubRouteObject [ ] ;
3353 meta ?: MetaFunction ;
3454 links ?: LinksFunction ;
3555}
3656
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+
3771interface StubNonIndexRouteObject
3872 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 { }
4884
4985type StubRouteObject = StubIndexRouteObject | StubNonIndexRouteObject ;
5086
@@ -141,6 +177,41 @@ export function createRoutesStub(
141177 } ;
142178}
143179
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+
144215function processRoutes (
145216 routes : StubRouteObject [ ] ,
146217 manifest : AssetsManifest ,
@@ -158,9 +229,15 @@ function processRoutes(
158229 id : route . id ,
159230 path : route . path ,
160231 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 ,
164241 action : route . action ,
165242 loader : route . loader ,
166243 handle : route . handle ,
@@ -193,8 +270,8 @@ function processRoutes(
193270
194271 // Add the route to routeModules
195272 routeModules [ route . id ] = {
196- default : route . Component || Outlet ,
197- ErrorBoundary : route . ErrorBoundary || undefined ,
273+ default : newRoute . Component || Outlet ,
274+ ErrorBoundary : newRoute . ErrorBoundary || undefined ,
198275 handle : route . handle ,
199276 links : route . links ,
200277 meta : route . meta ,
0 commit comments