I'm new to reactjs. I used this syntax that puts class active just single items but it does not work, How I can use ternary that get class active just for Dashboard. Could you please help me? also, I would like to use the different icon of fontawsome for each item of the menu, how I should pass icon of fontawsome in menuItems.
const { Component } = React;
class Sidebar extends Component {
constructor(props) {
super(props);
this.state = {
menuItems: [
"Dashboard" ,
"Customer",
"Category",
"Transaction",
"Pick-up",
"Stock",
"Financial",
"Report",
],
};
}
render() {
return (
<div className="wrapper d-flex align-items-stretch">
<nav id="sidebar">
<div className="custom-menu">
<button
type="button"
id="sidebarCollapse"
className="btn btn-primary"
>
<i className="fa fa-bars"></i>
<span className="sr-only">Toggle Menu</span>
</button>
</div>
<div className="p-4">
<h1>
<a href="index.html" className="logo">
Donyaro{" "}
</a>
</h1>
<ul className="list-unstyled components mb-5">
{this.state.menuItems&&this.state.menuItems.lenght?this.state.menuItems:[]}
{this.state.menuItems.map((item) => (
<li className={this.state.menuItems==="Dashboard" ? "active" :""} >
<a href={" "}>
<i className="fas fa-home mr-3"></i>
{item}
</a>
</li>
))}
</ul>
</div>
<div class="footer">
<a href={" "}>
<i className="fas fa-caret-down"></i>
</a>
<h6>
<span>Welcome, </span>
<a href="index.html" className="add-business">
Add Business
</a>
</h6>
</div>
</nav>
</div>
);
}
}
ReactDOM.render(<Sidebar />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>