@@ -198,7 +198,7 @@ const LocationAwareRouter: React.FC<RouterProps> = ({
198
198
)
199
199
}
200
200
201
- const { activeRouteTree , activeRoute , NotFoundPage } = analyzeRouteTree (
201
+ const { activeRoute , activePath , NotFoundPage } = analyzeRouterTree (
202
202
children ,
203
203
pathname ,
204
204
paramTypes
@@ -210,15 +210,14 @@ const LocationAwareRouter: React.FC<RouterProps> = ({
210
210
paramTypes = { paramTypes }
211
211
pageLoadingDelay = { pageLoadingDelay }
212
212
>
213
- < ParamsProvider path = { activeRoute ?. props ?. path } >
214
- { ! activeRoute && NotFoundPage ? (
213
+ < ParamsProvider path = { activePath } >
214
+ { ! activeRoute && NotFoundPage && (
215
215
< PageLoader
216
216
spec = { normalizePage ( NotFoundPage ) }
217
217
delay = { pageLoadingDelay }
218
218
/>
219
- ) : (
220
- activeRoute && activeRouteTree
221
219
) }
220
+ { activeRoute }
222
221
</ ParamsProvider >
223
222
</ RouterContextProvider >
224
223
)
@@ -234,49 +233,44 @@ const LocationAwareRouter: React.FC<RouterProps> = ({
234
233
* route, and in that case we don't need the NotFoundPage, so it doesn't
235
234
* matter.
236
235
*/
237
- function analyzeRouteTree (
236
+ function analyzeRouterTree (
238
237
children : React . ReactNode ,
239
238
pathname : string ,
240
239
paramTypes ?: Record < string , ParamType >
241
240
) {
242
- let activeRoute : React . ReactElement < InternalRouteProps > | undefined =
243
- undefined
244
241
let NotFoundPage : PageType | undefined = undefined
242
+ let activePath : string | undefined = undefined
245
243
246
244
function isActiveRoute ( route : React . ReactElement < InternalRouteProps > ) {
247
245
if ( route . props . path ) {
248
246
const { match } = matchPath ( route . props . path , pathname , paramTypes )
249
247
250
248
if ( match ) {
251
- // No need to loop further. As soon as we have a matching route we have
252
- // all the info we need
253
249
return true
254
250
}
255
251
}
256
252
257
- if ( route . props . notfound && route . props . page ) {
258
- NotFoundPage = route . props . page
259
- }
260
-
261
253
return false
262
254
}
263
255
264
- function analyzeRouteTreeInternal ( children : React . ReactNode ) {
265
- let active = false
266
-
267
- const activeRouteTree = React . Children . toArray ( children ) . reduce <
268
- React . ReactNode [ ]
269
- > ( ( acc , child ) => {
270
- if ( active ) {
271
- return acc
256
+ function analyzeRouterTreeInternal (
257
+ children : React . ReactNode
258
+ ) : React . ReactElement | undefined {
259
+ return React . Children . toArray ( children ) . reduce <
260
+ React . ReactElement | undefined
261
+ > ( ( previousValue , child ) => {
262
+ if ( previousValue ) {
263
+ return previousValue
272
264
}
273
265
274
266
if ( isRoute ( child ) ) {
267
+ if ( child . props . notfound && child . props . page ) {
268
+ NotFoundPage = child . props . page
269
+ }
270
+
275
271
// We have a <Route ...> element, let's check if it's the one we should
276
272
// render (i.e. the active route)
277
- active = isActiveRoute ( child )
278
-
279
- if ( active ) {
273
+ if ( isActiveRoute ( child ) ) {
280
274
// All <Route>s have a key that React has generated for them.
281
275
// Something like '.1', '.2', etc
282
276
// But we know we'll only ever render one <Route>, so we can give
@@ -289,38 +283,28 @@ function analyzeRouteTree(
289
283
key : '.rw-route' ,
290
284
} )
291
285
292
- // Keep this child. It's the last one we'll keep since `active` is `true`
293
- // now
294
- acc . push ( childWithKey )
286
+ activePath = child . props . path
295
287
296
- activeRoute = childWithKey
288
+ return childWithKey
297
289
}
298
290
} else if ( isReactElement ( child ) && child . props . children ) {
299
291
// We have a child element that's not a <Route ...>, and that has
300
292
// children. It's probably a <Set>. Recurse down one level
301
- const nestedChildren = analyzeRouteTreeInternal ( child . props . children )
302
-
303
- if ( nestedChildren . activeRouteTree . length > 0 ) {
304
- // We found something we wanted to keep. So let's push it to our
305
- // "active route tree"
306
- acc . push (
307
- React . cloneElement (
308
- child ,
309
- child . props ,
310
- nestedChildren . activeRouteTree
311
- )
312
- )
313
- active = true
293
+ const nestedActive = analyzeRouterTreeInternal ( child . props . children )
294
+
295
+ if ( nestedActive ) {
296
+ // We found something we wanted to keep. So let's return it
297
+ return React . cloneElement ( child , child . props , nestedActive )
314
298
}
315
299
}
316
300
317
- return acc
318
- } , [ ] )
319
-
320
- return { activeRouteTree, activeRoute, NotFoundPage }
301
+ return previousValue
302
+ } , undefined )
321
303
}
322
304
323
- return analyzeRouteTreeInternal ( children )
305
+ const activeRoute = analyzeRouterTreeInternal ( children )
306
+
307
+ return { activeRoute, activePath, NotFoundPage }
324
308
}
325
309
326
310
function isSpec ( specOrPage : Spec | React . ComponentType ) : specOrPage is Spec {
0 commit comments