Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

58
Vistas
React Modal returns last value of mapped data

Struggling with this issue with the earlier answers not utilizing map function / functional components. When I click my Card, the modal only shows the data of the last Modal:

export const ModalCard = (props) => {

    const productData = props.data;

    const [modal, setModal] = React.useState(false);
    const toggle = () => setModal(!modal);

    return (
        <Row>
            {productData.map((v, i) => (
                <Col className="py-4 btn" key={i} xs={12} md={4} lg={2}>
                    <div className="pb-4" onClick={toggle}>
                            <div className="product_card_padding">
                                <div className="pt-4">
                                    <span className="card_product_subtitle">{v.headline}</span>
                                </div>
                            </div>

                        <Modal isOpen={modal}
                            toggle={toggle}
                            centered
                        >
                            <ModalBody className="product_modal" onClick={toggle}>
                                <div className="row pt-3 bg_white">
                                    <Col>
                                        <div>
                                            <span className="card_product_subtitle">{v.headline}</span>
                                        </div>
                                    </Col>
                                </div>
                            </ModalBody>
                        </Modal>
                    </div>
                </Col>
            ))}
        </Row>

    );
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

According to your code, multiple modals will be opened and you will see the last modal.

If you have 10 products, then 10 modals will be opened.

My suggestion is that you need to define a global modal outside map function and you need to define a new state variable to represent the selected product to be rendered on the modal.

selectedInd holds the data index to be rendered on modal.

const [selectedInd, setSelectedInd] = React.useState(-1);

Then toggle function would be changed to set -1 to hide modal.

const toggle = () => setSelectedInd(-1);

And move the modal outside map.

Try to use the following code pattern.

export const ModalCard = (props) => {
  const productData = props.data;

  const [selectedInd, setSelectedInd] = React.useState(-1);
  const toggle = () => setSelectedInd(-1);
  const modal = selectedInd >= 0 && (productData && productData.length > selectedInd);

  return (
    <React.Fragment>
      <Row>
          {productData.map((v, i) => (
              <Col className="py-4 btn" key={i} xs={12} md={4} lg={2}>
                  <div className="pb-4" onClick={()=>setSelectedInd(i)}>
                      <div className="product_card_padding">
                          <div className="pt-4">
                              <span className="card_product_subtitle">{v.headline}</span>
                          </div>
                      </div>
                  </div>
              </Col>
          ))}
      </Row>
      {modal && <Modal isOpen={modal} toggle={toggle} centered>
          <ModalBody className="product_modal" onClick={toggle}>
              <div className="row pt-3 bg_white">
                  <Col>
                      <div>
                          <span className="card_product_subtitle">{productData[selectedInd].headline}</span>
                      </div>
                  </Col>
              </div>
          </ModalBody>
      </Modal>}
    </React.Fragment>
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda