I have the following react-router configuration
import React from 'react'
import { BrowserRouter, Route } from 'react-router-dom'
<BrowserRouter>
<Route exact path='/sign-in' component={SignIn} />
<Route exact path='/:username' component={Profile} />
</BrowserRouter>
When on a profile page like /dylan
the Profile component matches and :username param is "dylan" like I would expect.
When navigating to the /sign-in
route, the component gets rendered and the component get rendered too (with sign-in
as the username)
What is the idiomatic solution with react-router v4 to prevent routes from matching multiple components?
versions:
- react-router-dom 4.1.2
@Battle_Slug is right. It's actually better to differentiate the route
/sign-in
and/user/:username
But If you really need it, you can do it this way
Using
<Switch>
also right.Reference : https://reacttraining.com/react-router/web/api/Switch