您也可以使用
withRouter这样做,其结果与将代码放入
render参数中类似,并且避免了“假”的需要
<Route/>。
本质上,您将需要知道其位置的JSX放在了自己的组件中,该组件由包装
withRouter。这将位置提供给组件:
import { withRouter } from 'react-router-dom';const Content = withRouter(props => <div className={(props.location.pathname === "/account") ? "backg... ... </div>);然后在主要路由器部分中使用它:
class App extends Component{ render() { return ( <BrowserRouter> <Content/> ...


