我想僅在點擊物品上添加「活動」類屬性。到目前爲止,我已經叫狀態屬性activeItem:在React中添加「活動」類屬性以點擊物品
constructor(props) {
super(props);
this.state = { clickedNotebookId: '', activeItem: false };
}
我改變狀態設置爲true點擊函數內部:
renderContent(event) {
this.setState({clickedNotebookId: this.props.id, activeItem: true}, function(){
//display content
});
}
在我的渲染()方法,我做了如下:
<div className="list-group">
<a href="#" className={this.state.activeItem == true ? 'list-group-item active' : 'list-group-item'} onClick={this.renderContent}>{this.props.title}
<button className="pull-right btn btn-xs btn-danger" type="button" onClick={this.deleteNotebook}>
<strong>x</strong>
</button>
</a>
</div>
-
className={this.state.activeItem == true ? 'list-group-item active' : 'list-group-item'}
我知道上面的條件是錯誤的,因爲它在點擊時激活所有項目。理想情況下,它應該只突出顯示點擊的項目。
我很難把握這一點。你能解釋一下做得不那麼反應的方法嗎? – ZeroDarkThirty