You can also try alternatives of conditional rendering from here: Conditional Rendering in React and the JSX solution
Basically, the proposal is to create a "Conditional" Component, and use it like:
<Conditional if={this.state.something}>
<div>hello!</div>
<Conditional>
So, <div>hello!</div>
is going to be rendered only if this.state.something
is true.
In your example, it should be implemented in this way:
{
this.state.sdata.map((sdata, i) =>
<Conditional if={i < 5}>
<ServiceNavItem key={i} onClick={()=>this.handleSelect(i)}{...sdata} />
</Conditional>
}
Anyways, the best way to show only 5 elements is to do:
Basically, get only the first 5 elements from the array and do a .map
in that Array
.
{
this.state.sdata.slice(0, 5).map((sdata, i) =>
<ServiceNavItem key={i} onClick={()=>this.handleSelect(i)}{...sdata} />
);
}