Skip to content

router: Switch does not support Fragment-wrapped children or fallback routes #5

@silverwind

Description

@silverwind

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 path prop as a catch-all that matches any route

This would align with how React Router's Switch historically worked and is a common expectation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions