-
Notifications
You must be signed in to change notification settings - Fork 4
Description
This issue was written by an AI assistant (Claude) on behalf of @silverwind.
The current Switch implementation requires a flat array of Route elements and calls .find() directly on props.children. This has two limitations:
1. Fragment-wrapped children are not supported
When routes are grouped inside <>...</> fragments (common when conditionally rendering groups of routes), Switch sees the Fragment element itself rather than the Route children inside it.
<Switch>
<>
<Route path="/about" component={About} />
<Route path="/home" component={Home} />
</>
<Route path="/settings" component={Settings} />
</Switch>In this example, the Fragment is treated as a single child and child.props.path is undefined, so the routes inside it are never matched.
2. No fallback/catch-all route
A child without a path prop cannot act as a fallback. The current logic calls matchRoute(child.props.path, path) which returns falsy for undefined, so pathless children are always skipped. A common pattern is to place a fallback element as the last child of Switch without a path:
<Switch>
<Route path="/about" component={About} />
<Route path="/home" component={Home} />
<NotFound />
</Switch>Suggested behavior
- Recursively flatten Fragment children before matching
- Treat a child without a
pathprop as a catch-all that matches any route
This would align with how React Router's Switch historically worked and is a common expectation.