[英]How to change class when component is not equal to home than add class in header component in react.js?
當路線不在家時,我想在標題組件中添加一個類。 我的代碼是:
應用程序.js
<BrowserRouter>
<Header />
<Route exact path="/" component={Home}/>
<Route exact path="/shop" component={Shop}/>
<Route exact path="/contact" component={Contact}/>
<Footer />
</BrowserRouter>
Header.js 組件代碼:
<div className="all-category (here add-class when route is not equal to home)">
<h3 className="cat-heading"><i className="fa fa-bars" aria-hidden="true"></i>CATEGORIES</h3>
<ul className="main-category">
<li><a href="#">New Arrivals <i className="fa fa-angle-right" aria-hidden="true"></i></a>
<ul className="sub-category">
<li><a href="#">accessories</a></li>
<li><a href="#">best selling</a></li>
<li><a href="#">top 100 offer</a></li>
</ul>
</li>
</ul>
</div>
您可以檢查props.history.location.pathname
在頭組件同時確保包裹在頭組件withRouter
例如:
const Header = props => {
return <div className={props.history.location.pathname === '/anything' ? "YOUR_CLASS":""}>....</div>
}
export default withRouter(Header)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.