如何在使用React Router 4更改路由时呈现特定组件?

问题描述:

我有以下航线,如何在使用React Router 4更改路由时呈现特定组件?

<Route exact path ='/' component={Posts} /> 
<Route exact path ='/:category' component={Posts} /> 
<Route exact path ='/new' component={NewPost} /> 

的问题是,当我去/new路线,Post分量越来越与NewPost组件一起渲染为好。如何避免这一点?

我不得不在组件内换行。有效。

import { Route, Switch } from 'react-router-dom' 

<Switch> 
    <Route exact path ='/' component={Posts} /> 
    <Route exact path ='/:category' component={Posts} /> 
    <Route exact path ='/new' component={NewPost} /> 
</Switch>