i'm currently learning ReactJS, and im facing an issue while tryng to hide/show a div! The following code is working, but when i'm clicking the button "Learn More" it hide every description on every card, but i would like to show/hide the description only where i clicked it.
import Axios from 'axios';
import React, { useEffect, useState } from 'react';
import JobApplicationList from './JobApplicationList';
import Card from "react-bootstrap/Card";
import ScriptTag from 'react-script-tag';
export default class Home extends React.Component{
constructor(){
super()
this.state={
showMe:true,
advLists: []
}
}
operation(){
this.setState({
showMe:!this.state.showMe
})
}
componentDidMount() {
Axios.get(`http://localhost:3001/get/adv`)
.then(res => {
const advLists = res.data;
this.setState({ advLists });
})
}
render(){
return (
<div className="main-page">
<div className="adv_container">
{this.state.advLists.map((val, key) => {
return (
<div className="card_">
<Card style={{ width: "100%" }}>
<Card.Body>
<Card.Title>{val.compName}</Card.Title>
<Card.Subtitle className="mb-2 text-muted">
{val.city} | {val.compWebsite}
</Card.Subtitle>
<Card.Subtitle className="mb-2 text-muted">
{val.salaire} €
</Card.Subtitle>
{ this.state.showMe?
<div className="description">
<Card.Text>{val.description}</Card.Text>
</div>
:null
}
<Card.Link onClick={()=> this.operation()} id="toto" href="#">Learn more</Card.Link>
<Card.Link href="#">Apply</Card.Link>
</Card.Body>
</Card>
</div>
);
})}
</div>
</div>
)
}
}
I kinda know what's going wrong, when i'm pressing the button i give the showMe state to every card, but i dont know how to fix it! Thanks in advance for the help.