question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot create default exact Route component inside a Switch

See original GitHub issue

Version

4.2.0

Test Case

https://codesandbox.io/s/kxk110lqy3

Steps to reproduce

  1. Define an ExactRoute component:const ExactRoute = ({exact, ...rest}) => <Route exact={true} {...rest} />
  2. Use a bunch of Route inside a Switch (this bug does not apply when no Switch are used)
  3. 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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
timdorrcommented, Jul 5, 2018

I added a small clarification there.

1reaction
nfantonecommented, Jul 5, 2018

@timdorr

So, you can’t wrap a Route component and pass it to a Switch, unfortunately.

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.

<Switch>
      <Route
        exact
        path="/login"
        render={props =>
          isAuthenticated() ? (
            <Redirect to={{ pathname: '/myfiles', from: props.location }} />
          ) : (
            <LoginPage {...props} />
          )
        }
      />
      <Route exact path="/logout" component={LogoutPage} />
      <PrivateRoute exact path="/myfiles" component={MyWCABFilesPage} />
      <PrivateRoute exact path="/search" component={SearchPage} />
</Switch>

The above behaves just like you’d expect. PrivateRoute is a Route wrapper that redirects users to /login if they are not authenticated.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found