【发布时间】:2020-11-03 02:51:30
【问题描述】:
我正在尝试将 className 添加到我在 React 中映射的数组的第一个 div。我是新手,所以语法难倒我。
这是我的初始状态:
export default class EventList extends Component {
state = {
users: [],
events: [],
shownForm: null,
hideNewForm: true,
hideEditForm: true,
currentUserId: this.props.getCurrentUser(),
editTitle: "",
editDate: "",
editSynopsis: "",
editLocation: "",
editId: ""
}
这是一个不起作用的函数,但我正在考虑如何为数组使用条件语句:
addFirstClass =() => {
if(this.state.events === this.state.events[0]){
className = "card-body details"
}else{
className = "card-body details coral"
}
}
这里是我将 div 渲染到 DOM 的地方:
render() {
return(
<section className="events">
{this.state.events.map(event => (
<div key={event.id} className="card">
<div className={this.addFirstClass()}>
<p className="card-details">
Synopsis:{event.synopsis}</p>
</div>
</div>
</section>
)
}
我已经搜索了其他线程,但我似乎无法弄清楚如何进行这项工作。任何帮助表示赞赏!
【问题讨论】:
标签: reactjs