232

Can someone explain the difference between

<Route exact path="/" component={Home} />

and

<Route path="/" component={Home} />

I don't know the meaning of 'exact'

aarowman
  • 73
  • 1
  • 7
batt
  • 2,382
  • 2
  • 8
  • 11
  • 1
    The answers are all great. However one may get a doubt like "Why can not we have exact for all the routes then?" Imagine a URL that is something like this. https://yourreactwebsite.com/getUsers/userId=? This is an example where the user's ID will be dynamically fed in the URL and so we can not go with exact prop in your Router here. – VIJAYKUMAR REDDY ALAVALA Jul 18 '19 at 09:15

6 Answers6

368

In this example, nothing really. The exact param comes into play when you have multiple paths that have similar names:

For example, imagine we had a Users component that displayed a list of users. We also have a CreateUser component that is used to create users. The url for CreateUsers should be nested under Users. So our setup could look something like this:

<Switch>
  <Route path="/users" component={Users} />
  <Route path="/users/create" component={CreateUser} />
</Switch>

Now the problem here, when we go to http://app.com/users the router will go through all of our defined routes and return the FIRST match it finds. So in this case, it would find the Users route first and then return it. All good.

But, if we went to http://app.com/users/create, it would again go through all of our defined routes and return the FIRST match it finds. React router does partial matching, so /users partially matches /users/create, so it would incorrectly return the Users route again!

The exact param disables the partial matching for a route and makes sure that it only returns the route if the path is an EXACT match to the current url.

So in this case, we should add exact to our Users route so that it will only match on /users:

<Switch>
  <Route exact path="/users" component={Users} />
  <Route path="/users/create" component={CreateUser} />
</Switch>

The docs explain exact in detail and give other examples.

Chase DeAnda
  • 13,160
  • 2
  • 26
  • 39
  • 13
    "But, if we went to http://app.com/users/create, it would again go through all of our defined routes and return the FIRST match it finds." - in fact it will return all the Routes for which it found a match (full or partial). The behavior described by @Chase DeAnda will happen only if the the 's are enclosed by a tag. – watsabitz Nov 24 '18 at 11:50
  • 7
    `exact` should be the default in my opinion – Alexander Derck Mar 12 '19 at 20:23
  • What about if we have each route definition in different components, I mean `/admin//users` in Admin component, and `/admin/users/create` in the Root component??? I'm currently have this situation and the typical `exact` solution do not work properly. – Yulio Aleman Jimenez Sep 27 '19 at 19:32
  • I think this behavior only works if both routes are in the same level of its Switch parent (or component) – Yulio Aleman Jimenez Sep 27 '19 at 20:02
  • @YulioAlemanJimenez The highest level route definition will always win. So in the root if you have `/admin/users/create`, router will select that route instead of `/admin/users` which is nested in the Admin component because the Admin component was never rendered. – Chase DeAnda Sep 27 '19 at 20:38
  • 1
    @ChaseDeAnda what I need is exactly the opposite. Maybe I should write a new answer on SO to clarify my situation and get properly answers. – Yulio Aleman Jimenez Sep 28 '19 at 17:22
  • This answer is wrong. Unless is used, all the matching routes are returned. The very purpose of switch is to render only the first or that matches. – chetan Jun 04 '20 at 15:38
18

In short, if you have multiple routes defined for your app's routing, enclosed with Switch component like this;

<Switch>

  <Route exact path="/" component={Home} />
  <Route path="/detail" component={Detail} />

  <Route exact path="/functions" component={Functions} />
  <Route path="/functions/:functionName" component={FunctionDetails} />

</Switch>

Then you have to put exact keyword to the Route which it's path is also included by another Route's path. For example home path / is included in all paths so it needs to have exact keyword to differentiate it from other paths which start with /. The reason is also similar to /functions path. If you want to use another route path like /functions-detail or /functions/open-door which includes /functions in it then you need to use exact for the /functions route.

milkersarac
  • 3,064
  • 3
  • 26
  • 30
  • 3
    Actually the second part explains it. Let's say if you have 2 routes like `/motor` and `/motorbike` then you need to put `exact` to the route with path `/motor`. Otherwise, both `/motor` and `/motorbike` routes pick up the component with path `/motor`. – milkersarac Sep 21 '20 at 07:13
8

Take a look here: https://reacttraining.com/react-router/core/api/Route/exact-bool

exact: bool

When true, will only match if the path matches the location.pathname exactly.

**path**    **location.pathname**   **exact**   **matches?**

/one        /one/two                true        no
/one        /one/two                false       yes
aarowman
  • 73
  • 1
  • 7
Nouar
  • 2,261
  • 1
  • 16
  • 17
-1

Please try this.

       <Router>
          <div>
            <Route exact path="/" component={Home} />
            <Route path="/news" component={NewsFeed} />
          </div>
        </Router> 

            
-2

By using exact, you can make sure that the contents of the homepage component will not appear on the other pages.

This is the scenario without using exact:

HOMEPAGE

Location: /

-----------------
homepage content
-----------------

SECOND PAGE

Location: /second-page

-----------------
homepage content
-----------------
-----------------
second content
-----------------

==========================================

Using exact:

HOMEPAGE

Location: /

-----------------
homepage content
-----------------

SECOND PAGE

Location: /second-page

-----------------
second content
-----------------
-4

The shortest answer is

Please try this.

<switch>
   <Route exact path="/" component={Home} />
   <Route path="/about" component={About} />
   <Route path="/shop" component={Shop} />
 </switch>
  • 4
    This does basically nothing to explain the meaning of the `exact` attribute/prop, and thus is surely not an "answer". You should try to address the question actually being asked rather than to give a solution to a problem which you are not sure OP actually has. – Victor Zamanian Jul 14 '20 at 12:19