0

I've few images around 15 which I rendering to my main component. It's working fine but I'm unable to pass a single image to my modal. When I click on any card all those 15 images are overlapping in the modal wherever I want the only clicked image to be passed in the modal. How do I achieve it?

In short: Only clicked image should be rendered on modal but when I click on any card it's rendering all the images in modal at once

Here is my live code link available for codesandbox

Below is my raw code

App.js

import { Grid } from "@material-ui/core";
import MyCard from "./components/MyCard";
import React from "react";
import ImagePopup from "./components/ImagePopup";
import HairSamples from "./assets/Images";

export default function App() {
  const [openModal, setOpenModal] = React.useState(false);
  // state should load all my images
  const [img, setImg] = React.useState(HairSamples);

  const handleOpen = () => {
    setOpenModal(true);
  };

  const handleClose = () => {
    setOpenModal(false);
  };
  return (
    <div>
      <Grid container spacing={2}>
        {img.map((item, i) => (
          <Grid item xs={12} sm={2} key={i}>
            <MyCard img={item} handleOpen={handleOpen} />
            {/* it should only render clicked image but it's rendering all my images at once */}
            <ImagePopup
              img={item}
              openModal={openModal}
              setOpenModal={setOpenModal}
              handleOpen={handleOpen}
              handleClose={handleClose}
            />
          </Grid>
        ))}
      </Grid>
    </div>
  );
}
user9824674
  • 493
  • 1
  • 14
  • 28
  • You asking how to manage multiple states, you need to have an open model state for each image, see duplicate – Dennis Vash May 19 '21 at 10:46
  • Hey, you didn't understand my question. read it twice. I wasn't asking for the state but for modal handleOpen method which should open only clicked image – user9824674 May 19 '21 at 10:49
  • 1
    @user9824674 Please follow this link: https://codesandbox.io/s/dazzling-monad-2guu4 I have solve your question but I can not post answer but link will help you. thanks! – Priyank Kachhela May 19 '21 at 10:50
  • 1
    The answer for your question https://codesandbox.io/s/ecstatic-feynman-umofo?file=/src/App.js – Matvii Hodovaniuk May 19 '21 at 10:50
  • 1
    Thank you so much, guys. I really appreciate your help. That guy didn't even read my question and just closed it. But still, you guys helped me. Thank you sooo much @Priyank and Matvii – user9824674 May 19 '21 at 10:53

0 Answers0