Answer the question
In order to leave comments, you need to log in
How to conditionally add react attributes?
Good day.
There is a component that is responsible for displaying pagination.
In the return of the component, pagination is output as follows:
return (
<div className = "Question-list">
{this.props.data.map((value, index) => <li onClick = {this.setChosenQuestionNumber}
key = {this.props.data[index]['id']}
className="Question-list-item">
<span className="question-list-number">{index + 1}
</span>
</li>)}
</div>
)
<li>
Answer the question
In order to leave comments, you need to log in
Option 1 :
where someCondition is a condition, for example:
You pass an element as the first argument in the map call , but you access it through an array:
a very illogical move, correct it to:key = {value.id}
return (
<div className="Question-list">
{this.props.data.map((value, index) => {
const cn = []
if (value === 1) {
cn.push('Question-list-item--current')
}
else {
cn.push('Question-list-item')
}
return <li onClick={this.setChosenQuestionNumber} key={value.id} className={cn.join(' ')}>
<span className="question-list-number">{index + 1}</span>
</li>
})}
</div>
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question