All Route
components need to be a direct child of a Switch
in order to work properly. Per the docs:
<Switch>
is unique in that it renders a route exclusively. In contrast, every <Route>
that matches the location renders inclusively.
What's happening here is that you have MainLayout
interrupting the parent/child relationship between your Switch
and your Route
components. Therefore the Route
s resort to their default behavior which is to render all Route
s that match the path instead of just the first.
You need to introduce an additional Switch
component inside of MainLayout
so that the Routes
have a Switch
as their parent. Additionally, you should replace the Fragment
inside of the isAuthenticated
branch with a Switch
.
const Root = () =>
<BrowserRouter>
<Switch>
<ProtectedRoute exact path="/:username/contact-networks" component={ContactNetworks} />
<ProtectedRoute exact path="/:username/tags" component={UserTags} />
<ProtectedRoute exact path="/:username/profile-picture" component={UserProfilePicture} />
<MainLayout nofooter={['/login']}>
<Switch>
<Route exact path="/" component={Home} />
{!isAuthenticated ? (
<Switch>
<Route exact path="/logup" component={Logup} />
<Route exact path="/login" component={Login} />
</Switch>) : <Redirect to="/" />}
<Route exact path="/users/:username" component={UserProfile} />
<Route exact path="/:username/catalog/:product" component={ProductPage} />
<ProtectedRoute exact path="/:username/products/new" component={PostProduct} />
<Route exact path="/:username/opinions" component={ClientsOpinions} />
<ProtectedRoute exact path="/:username/opinions/new" component={NewOpinion} />
<Route exact path="/:shop/contact" component={ShopContact} />
<Route exact path="/search/results" component={SearchResults} />
<Route component={NotFound} />
</Switch>
</MainLayout>
</Switch>
</BrowserRouter >
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…