# React-Router-DOM path match

**URL:** <https://forum.freecodecamp.org/t/react-router-dom-path-match/351858>\
**Category:** General\
**Created:** [February 20, 2020, 12:19pm UTC](https://forum.freecodecamp.org/t/react-router-dom-path-match/351858 "2020-02-20T12:19:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [February 20, 2020, 12:19pm UTC](https://forum.freecodecamp.org/t/react-router-dom-path-match/351858/1 "2020-02-20T12:19:54Z")

</div>

Hello all.

I have started going over how to use React with _react-router-dom_, and I am interested in understanding the reason for the `Route` component matching any _path_.  
For example:

```auto
<Route path="/" component={Home} />
<Route path="/about" component={About} />

```

I understand that on the _root_ path, only the `Home` component is rendered, and then on the _about_ path, both the `Home` and `About` components are rendered.

This can be avoided by using the `exact` keyword.

**Question** : Why would this behaviour even exist? When would one not want to have the _path_ match exactly?

---

<div class="post-metadata">

**Author:** ![Osiris](https://avatars.discourse-cdn.com/v4/letter/o/839c29/32.png) [@Osiris](https://forum.freecodecamp.org/u/Osiris)\
**Post date:** [February 20, 2020, 1:26pm UTC](https://forum.freecodecamp.org/t/react-router-dom-path-match/351858/2 "2020-02-20T13:26:14Z")

</div>

Well an example might be if you have a blog and you have a route like `/posts` and then another route that might be `/posts/:postId` and you have some posts displayed and maybe somewhere beneath them a full post, a specific one. Might be more cases but ye mostly it s not used like this I guess.

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [February 20, 2020, 1:57pm UTC](https://forum.freecodecamp.org/t/react-router-dom-path-match/351858/3 "2020-02-20T13:57:35Z")

</div>

You might have the `/` route as a default route (least specific) on the bottom in a Switch when nothing else matches.

> [Route Matchers](https://reacttraining.com/react-router/web/guides/primary-components/route-matchers)
> 
> One important thing to note is that a `<Route path>` matches the **beginning** of the URL, not the whole thing. So a `<Route path="/">` will **always** match the URL. Because of this, we typically put this `<Route>` last in our `<Switch>`. Another possible solution is to use `<Route exact path="/">` which **does** match the entire URL.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [February 20, 2020, 2:04pm UTC](https://forum.freecodecamp.org/t/react-router-dom-path-match/351858/4 "2020-02-20T14:04:36Z")

</div>

Thank you. I did not think about this.

I somehow missed that when `Switch` is used, the first, and only the first, is matched and rendered.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:35pm UTC](https://forum.freecodecamp.org/t/react-router-dom-path-match/351858/5 "2021-06-01T19:35:20Z")

</div>

Closed on June 1, 2021, 7:35pm UTC


