Answer the question
In order to leave comments, you need to log in
How to determine the index of the element that was clicked?
this.state = {
selectCard: {id: null, select: false}
};
handleOnClick(){
let cards = document.querySelectorAll('#fabricCard'); //собираем массив все карточек
cards.forEach((card, i) => card.addEventListener('click', () => {
this.saveData(i);
}));
}
saveData(i){
this.setState({
selectCard: {
id: i,
select: !this.state.selectCard.select
}
});
}
render{
return(
<div className={styles.expand_container}>
{_mock_.free_fabric.map(card =>
<FreeFabricCard key={card.id}{...card} amountCard={amountCard} handleClick={this.handleOnClick} choose={selectCard.select}/>
)}
</div>
)
}
Answer the question
In order to leave comments, you need to log in
render{
return(
<div className={styles.expand_container}>
{_mock_.free_fabric.map((card,index) =>
<FreeFabricCard key={card.id}{...card} amountCard={amountCard} handleClick={this.handleOnClick.bind(index)} choose={selectCard.select}/>
)}
</div>
)
}
handleOnClick(ind){
console.log("your index",ind)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question