I am trying to grab the title of the list item when the user clicks the "Save Card" button and set it to a state. I have tried a few different ways but the console log keeps showing a blank line.
This is how the list is being populated:
<Col sm="12" md="7">
<h1 style={{ textAlign: 'center', color: '#fff' }}>Results</h1>
{this.state.cards.length ? (
<List>
{console.log(this.state.cards)};
{this.state.cards.map((card) => {
return (
<ListItem style={{ textAlign: 'left' }} key={card.name}>
{card.layout === 'transform' ? (
<div>
<img
className="cardImg"
src={card.card_faces[1].image_uris.small}
alt={card.card_faces[1].name}
/>
<img
className="cardImg"
src={card.card_faces[0].image_uris.small}
alt={card.card_faces[0].name}
/>
</div>
) : (
<img
className="cardImg"
src={card.image_uris.small}
alt={card.name}
/>
)}
<h4>{card.name}</h4>
<h6>{card.mana_cost}</h6>
<h6>{card.type_line}</h6>
<h6>{card.set_name}</h6>
<h6>{card.rarity}</h6>
{card.layout === 'transform' ? (
<div>
<p>
{card.card_faces[0].name}: {card.card_faces[0].oracle_text}
</p>
<p>
{card.card_faces[1].name}: {card.card_faces[1].oracle_text}
</p>
</div>
) : (
<p>{card.oracle_text}</p>
)}
<p>Commander Legality: {card.legalities.commander}</p>
<p>Price in USD: {this.checkPrice(card.prices.usd)}</p>
<p>Foil Price in USD: {this.checkPrice(card.prices.usd_foil)}</p>
{sessionStorage.getItem('user') !== null ||
this.state.username !== '' ? (
<SaveBtn
onClick={this.saveCard}
className="submitBtn save-btn btn"
style={{ backgroundColor: '#4e7781', color: '#fff' }}
/>
) : (
''
)}
</ListItem>
)
})}
</List>
) : (
<h3 style={{ textAlign: 'center', color: '#fff' }}>
No Results to Display
</h3>
)}
</Col>
This is how I'm currently trying to get the name:
saveCard = (event) => {
this.setState({
cardName: this.key
});
console.log(this.state.cardName)
API.SaveCard({
name: this.cardName
})
.then((response) => {
console.log("saved following card")
console.log(response)
})
};
Any help would be appreciated, here is also a link to my repo this code is located in the Database.js in the pages folder within src. https://github.com/EricVincitore/command-central