Reputation: 362
How could I access to the element in state array for example
this.state = {
items: [1,5,22,6],
}
here I'm trying to set up a class if the id exists in items state
{this.props.data.map((row)=> (
<ul>
<li key={row.id} className={row.id === this.state.items ? 'complete' : 'pending'}" >{row.name}</li>
</ul>
))}
Upvotes: 0
Views: 6431
Reputation: 993
You can use the .includes() function for arrays:
{this.props.data.map((row)=> (
<ul>
<li key={row.id} className={this.state.items.includes(row.id) ? 'complete' : 'pending' }>{row.name}</li>
</ul>
))}
Alternatively use .indexOf():
{this.props.data.map((row)=> (
<ul>
<li key={row.id} className={this.state.items.indexOf(row.id) !== -1 ? 'complete' : 'pending' }>{row.name}</li>
</ul>
))}
Upvotes: 3