DEV Community

Cover image for Exact path in react-router
Aastha Pandey
Aastha Pandey

Posted on

1

Exact path in react-router

When I was using react router for the first time I used path instead of exact path as props in component.

<Router>
  <Switch>
          <Route exact path="/">
            <Home />
          </Route>
          <Route path="/about">
            <About />
          </Route>
          <Route path="/dashboard">
            <Dashboard />
          </Route>
        </Switch>
      </div>
    </Router>
Enter fullscreen mode Exit fullscreen mode

In the above code when one replaces exact path to path it will match every path starting with '/', since the is inside the so, it will match the first path and don't check for the other matches.
The above code after removing exact from the will always show component and ignore or .

If one doesn't want to use exact path then the below code will work

<Router>
  <Switch>
     <Route path="/about">
            <About />
          </Route>
          <Route path="/dashboard">
            <Dashboard />
          </Route>
         <Route path="/">
            <Home />
          </Route>
        </Switch>
      </div>
    </Router>
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay