1
- import { h , Fragment , createContext , cloneElement , toChildArray } from 'preact' ;
1
+ import { h , createContext , cloneElement , toChildArray } from 'preact' ;
2
2
import { useContext , useMemo , useReducer , useLayoutEffect , useRef } from 'preact/hooks' ;
3
3
4
4
/**
@@ -137,6 +137,7 @@ export function Router(props) {
137
137
if ( ! url ) {
138
138
throw new Error ( `preact-iso's <Router> must be used within a <LocationProvider>, see: https://github.com/preactjs/preact-iso#locationprovider` ) ;
139
139
}
140
+ const { rest = path } = useContext ( RouterContext ) ;
140
141
141
142
const isLoading = useRef ( false ) ;
142
143
const prevRoute = useRef ( path ) ;
@@ -156,7 +157,7 @@ export function Router(props) {
156
157
157
158
let pathRoute , defaultRoute , matchProps ;
158
159
toChildArray ( props . children ) . some ( ( /** @type {VNode<any> } */ vnode ) => {
159
- const matches = exec ( path , vnode . props . path , ( matchProps = { ...vnode . props , path, pathParams, searchParams } ) ) ;
160
+ const matches = exec ( rest , vnode . props . path , ( matchProps = { ...vnode . props , path : rest , pathParams, searchParams } ) ) ;
160
161
if ( matches ) return ( pathRoute = cloneElement ( vnode , matchProps ) ) ;
161
162
if ( vnode . props . default ) defaultRoute = cloneElement ( vnode , matchProps ) ;
162
163
} ) ;
@@ -169,7 +170,7 @@ export function Router(props) {
169
170
const routeChanged = useMemo ( ( ) => {
170
171
prev . current = cur . current ;
171
172
172
- cur . current = /** @type {VNode<any> } */ ( h ( Fragment , { key : path } , incoming ) ) ;
173
+ cur . current = /** @type {VNode<any> } */ ( h ( RouterContext . Provider , { value : matchProps } , incoming ) ) ;
173
174
174
175
// Only mark as an update if the route component changed.
175
176
const outgoing = prev . current && prev . current . props . children ;
@@ -283,6 +284,9 @@ Router.Provider = LocationProvider;
283
284
LocationProvider . ctx = createContext (
284
285
/** @type {import('./router.d.ts').LocationHook & { wasPush: boolean } } */ ( { } )
285
286
) ;
287
+ const RouterContext = createContext (
288
+ /** @type {{ rest: string } } */ ( { } )
289
+ ) ;
286
290
287
291
export const Route = props => h ( props . component , props ) ;
288
292
0 commit comments