So I am mapping over an array of items, and am wanting to open up a unique Modal for whichever one is clicked. For some reason, if I click any of the items, a Modal pops up for each of the items. Here is my code
const PrivateProjects = props => {
const [show, setShow] = useState(false);
const openModal = () => {
setShow(true)
}
return (
<div className='projectContainer'>
{privateProjects.map((project, index) => (
<div className='lightbox' onClick={() => {openModal()}}>
<div className='project'>
<h5>{project.name}</h5>
<img src={project.image} alt='hibiscus project' />
</div>
<MyModal title={project.name} img={project.image} show={show} onHide={()=> {setShow(null)}}/>
</div>
))}
</div>
)
}
export default PrivateProjects;
Here is MyModal
import React from 'react';
import Modal from 'react-bootstrap/Modal';
import {Button} from 'react-bootstrap';
const MyModal = (props) => {
console.log(props);
return (
<Modal
{...props}
size="lg"
aria-labelledby="contained-modal-title-vcenter"
centered
>
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter">
{props.title}
</Modal.Title>
</Modal.Header>
<Modal.Body>
<img src={props.img} />
<p>
Cras mattis consectetur purus sit amet fermentum. Cras justo odio,
dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac
consectetur ac, vestibulum at eros.
</p>
</Modal.Body>
<Modal.Footer>
<Button onClick={props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
);
}
export default MyModal;
Also, the buttons to close the modal aren't working either, only the escape key works. Not sure where I am messing up with that.
P.S. I took out the imports and array of objects for the sake of visibility, because it's a lot of data and works fine.
