I'm having some difficulty with grabbing specific data from list items that are coming from an API that is being mapped using React.
Here in my render I'm mapping over a list of artists and based on that it'll render the artists name, song title and the URL of the sheet music:
render(){
return(
<div className="songContainerOuter">
{this.props.artist.map((artist, i) => {
return (
<div className="songContainerInner" key={i}>
<ul>
<div className="leftSide">
<li ref={ref => this.artistName = ref}>{artist}</li>
</div>
<div className="rightSide">
<div className="topRight">
<li ref={ref => this.songTitle = ref}>{this.props.title[i]}</li>
</div>
<div className="bottomRight">
<li ref={ref => this.songUrl = ref}><a href={`${this.props.link[i]}`}><i className="fas fa-link"></i></a></li>
<li onClick={this.addFavourite}><i className="fas fa-plus"></i></li>
</div>
</div>
</ul>
</div>
)
})}
</div>
)
}
Then on click of an li which is a plus icon, I want to store the values in each individually rendered div. The problem I'm having is that when I click on the add button, in my console log I'm getting all of the data back and the exact same song, artist and URL regardless of which mapped div add button I'm clicking on:
addFavourite(e){
e.preventDefault();
const favs ={
artist: this.artistName.textContent,
title: this.songTitle.textContent,
url: this.songUrl.textContent.outerHTML,
};
const newFavs = Array.from(this.state.favourites);
newFavs.push(favs);
this.setState({
favourites: newFavs
})
console.log(this.state.favourites)
}
Is there an alternative way to store the values within the li tags and store ONLY the values uniquely within each mapped div?