How can I make separate modals with different descriptions?
P粉832490510
2023-08-17 16:25:08
<p>I'm making a car dealer website and when the user clicks the button below the picture of the car, I want the modal to display only information about that car. So far I have successfully created multiple modal buttons, but they all display the same information. </p>
<p>How do I create separate modals with different information? </p>
<pre class="brush:js;toolbar:false;">import "./Modal.css";
import Pagani from '../Images/pagani2.jpg';
import Pagani1 from '../Images/pagani.jpg';
import p1 from '../Images/p1.jpg';
import r342 from '../Images/r342.jpg';
import * as React from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';
import Modal from '@mui/material/Modal';
const style = {
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 400,
bgcolor: 'background.paper',
border: '2px solid #000',
boxShadow: 24,
p: 4,
};
export default function BasicModal() {
const [open, setOpen] = React.useState(false);
const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
return (
<div>
<Button onClick={handleOpen}>Open modal box</Button>
<Modal
open={open}
onClose={handleClose}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box sx={style}>
<Typography id="modal-modal-title" variant="h6" component="h2">
Text in modal box
</Typography>
<Typography id="modal-modal-description" sx={{ mt: 2 }}>
Duis mollis, est non commodo luctus, nisi erat portitor ligula.
</Typography>
</Box>
</Modal>
</div>
); }
</pre>
You can create a more generic component for the modal that accepts props to customize the content of each car
Create a new component named
CarModal
that acceptscarImage
,carTitle
, andcarDescription
as props.Now you can use this component multiple times inside the
Cars
component (the one where you want to show the cars) to create separate modals for each car, you just need to pass the appropriate props