I am building a website that allows a user to create a job opening, assign to it different requirements and delete it.
I have two main components:
HRdashboard
is the parent component in which all the job openings are listed.MyOpeningCard
is the card that contains information of a single job opening. It comes from the.map
function in the parent component:
HRdashboard
function HRdashboard({changeAuthLoginHR}) {
const [HRuser, setHRuser] = React.useState([]);
const [openings, setOpenings] = React.useState([]);
useEffect( ()=> {
let unmountedOpenings = false
async function getAllOpenings(){
let response = await axios("http://www.localhost:3000/api/v1/openings");
if(!unmountedOpenings)
setOpenings(response.data)
}
let jwt = window.localStorage.getItem('jwt')
let result = jwtDecode(jwt)
setHRuser(result)
changeAuthLoginHR()
getAllOpenings()
return () => {
unmountedOpenings = true
}
}, [],
)
return(
<div>
<Container style={{marginTop: "50px"}}>
<Row>
{openings.map(opening =>
<Col>
<MyOpeningCard key={opening.id} opening={opening} /> # Here is where the child is
</Col>
)}
</Row>
</Container>
</div>
)
}
MyOpeningCard
function MyOpeningCard({opening}) {
return(
<div>
<Card style={{ width: '18rem', marginTop: "15px"}}>
<Card.Body>
<Card.Title>{opening.title}</Card.Title>
<Card.Subtitle className="mb-2 text-muted">Requirements: </Card.Subtitle>
<Card.Text>
{opening.requirements[0].requirements.map(requirement => <li>{requirement}</li>)}
</Card.Text>
<div>
<Card.Link href="#" onClick={console.log(opening.id)}>Click Here</Card.Link> # If I click here, it console.log all the IDs, not only this opening id.
</div>
</Card.Body>
</Card>
</div>
)
}
My question:
If I click on Click Here
in my child component, it triggers console.log(opening.id)
.
I was expecting to see in console only the opening.id
of the Job Opening I click. However, I see all the ids of the openings.
Why is it so?