【发布时间】:2020-05-24 05:56:11
【问题描述】:
我能够将数据从 json 文件映射到卡片,并且在卡片页脚中我有一个按钮来切换模式,但我一直得到相同的模式。在做了一些研究之后,我发现您无法映射模态,因为您最终会生成所有模态并且它会显示最后一个。我只是不知道怎么做,所以我只得到正确的。
import projects from '../../projects.json';
const Portfolio = () => {
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
return (
<section id='portfolio'>
<Container>
<Row>
{projects.map(project => (
<Col lg='4' sm='6' key={project.id}>
<div>
<Card className='text-center mb-2'>
<CardImg
top
height='100%'
width='100%'
src={require('../../img' + project.image)}
alt={project.title}
/>
<CardBody>
<CardTitle>{project.title}</CardTitle>
<CardText>{project.description}</CardText>
<Row className='justify-content-center'>
<Button color='info' onClick={() => toggle(project.id)}>
My Resume
</Button>
<Modal isOpen={modal} toggle={toggle} size='lg'>
<ModalBody size='lg'>
<img
className='img-fluid'
style={{
width: '100%',
height: '100%'
}}
title='resume'
alt={project.title}
src={require('../../img' + project.image)}
/>
<p>{project.description}</p>
</ModalBody>
<ModalFooter>
<Button
color='info'
className='mr-2 d-flex align-items-center'
href={project.href}
target='_blank'
rel='noopener noreferrer'
>
Check it out
</Button>
<a
className='d-block'
href={project.github}
target='_blank'
rel='noopener noreferrer'
>
<i className='fab fa-github fa-3x text-muted'></i>
</a>
<Button
color='secondary'
className='btn btn-primary btn-xl bg-info'
size='sm'
onClick={toggle}
>
Close
</Button>
</ModalFooter>
</Modal>
</Row>
</CardBody>
</Card>
</div>
</Col>
))}
</Row>
</Container>
</section>
);
};
【问题讨论】:
标签: reactjs modal-dialog mapping react-hooks reactstrap