Cannot create default exact Route component inside a Switch
See original GitHub issueVersion
4.2.0
Test Case
https://codesandbox.io/s/kxk110lqy3
Steps to reproduce
- Define an
ExactRoutecomponent:const ExactRoute = ({exact, ...rest}) => <Route exact={true} {...rest} /> - Use a bunch of
Routeinside aSwitch(this bug does not apply when noSwitchare used) - Among those routes, define one using
ExactRoute, for instance:<ExactRoute path="/about" component={About} />
Expected Behavior
I am trying to circumvent the fact that exact defaults to false (see #4958) by creating my own ExactRoute component. I would expect my ExactRoute component to match only if it is an exact match.
Actual Behavior
If you go to /about/me (a non-exact match), the About component will render
Alternatively, the matching will be exact only if exact is an argument of ExactRoute: <ExactRoute exact path="/about" component={About} />
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Cannot create default exact Route component inside a Switch
Switch reads the props of its children directly. So, a dirty little secret is that you can actually put any component under it...
Read more >React Router Switch and exact path - Stack Overflow
In your case, home path is set to exact. <Route exact path="/" component={Home} /> <Route path ...
Read more >Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide, including a review of additions and improvements from v5.
Read more >How to get React Router 4 to allow nested components inside ...
Create a component to group routes in React Router 4. ... import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; ...
Read more >How to Set React Router Default Route Redirect to /home
In this guide, you will learn how to get started with routing and ... Switch components are used to render the default components...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I added a small clarification there.
@timdorr
May I ask again why not? We are doing this in pretty much every project we have and seems to be working fine for us.
The above behaves just like you’d expect.
PrivateRouteis aRoutewrapper that redirects users to/loginif they are not authenticated.