When multiple routes could match the same URL, React Router v5 follows these precedence rules:
1. Exact Path Matches Win
Routes with exact prop take highest priority.
Example:
<Route exact path="/about" component={About} /> // Wins for "/about"
<Route path="/about/team" component={Team} /> // Ignored for "/about"
2. Path Specificity Matters
More specific paths are prioritized over generic ones.
Example:
<Route path="/users/:id" component={UserDetail} />
<Route path="/users/new" component={NewUser} /> // Wins for "/users/new" (more specific)
3. Order of Declaration
If two routes have equal specificity, the first one defined wins.
Example:
<Route path="/shop" component={Shop} /> // Renders for "/shop"
<Route path="/shop" component={DiscountShop} /> // Ignored (defined later)
4. <Switch> Component Enforces Single Match
Wrapping routes in <Switch> makes React Router pick only the first matching route.
Without <Switch>, all matching routes render.
Example:
<Switch>
<Route path="/users/new" component={NewUser} /> // Only this renders for "/users/new"
<Route path="/users/:id" component={UserDetail} />
</Switch>