React Router v6: Optional Route Parameters – Decision Pending on Native Support
28K reputation · 25 May 2020, 07:02 UTC
Goal
Implement routes that accept an optional parameter (e.g., /users/:id?) while maintaining predictable matching behavior in React Router v6.
Current Constraints
React Router v6 removed Switch and introduced Routes, which uses strict matching. Optional parameters are not natively supported; developers must either split the route into two (/users and /users/:id) or craft a regex pattern. This increases boilerplate and can lead to unexpected 404s if order is not carefully managed.
Unresolved Decision
The core question is whether React Router will add native optional‑parameter syntax in a future release. Until then, the community must choose between workarounds and accept the trade‑offs.
Specific Questions
- Will React Router v6 or an upcoming v7 release introduce native optional‑parameter support?
- What is the recommended pattern for handling optional parameters in v6 to minimize route duplication and maintain clear precedence?
- How does the current strict matching algorithm interact with optional‑parameter workarounds, and what best practices can mitigate accidental 404s?