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

272
Vistas
React Semantic UI Modal closeicon doesn't work

I want to make a modal for my App. I've tried everything, and my closeicon won't close my modal. It works fine when I do modal in the same file, but I want make my modal as separate component. Is something wrong with my code?

Parent Component:

import { Card, Placeholder, Image, Icon } from "semantic-ui-react";
import BookModal from "./BookModal";
import { useState } from "react";

const BookCard = props => {

    const [showModal, setShowModal] = useState(false)


    return (
        <Card onClick={() => setShowModal(true)}>
            <Image><Placeholder><Image src={props.image} size="large" /></Placeholder></Image>
            <Card.Content>
                <Card.Header>{props.title}</Card.Header>
                <Card.Meta>
                    <span className='date'>{props.publishedDate}</span>
                </Card.Meta>
                <Card.Description>{props.author}</Card.Description>
            </Card.Content>
            <Card.Content extra>
                <Icon name='star'>Rating</Icon>    
            </Card.Content>
            <BookModal key="Modal1" open={showModal} onClose={() => setShowModal(false)}/>
        </Card>
     );
}
 
export default BookCard;

Children Component Modal:

import { Modal } from "semantic-ui-react";

const BookModal = props => {

    return ( 
        <Modal
          closeIcon
          onClose={props.onClose}
          open={props.open} >

            <Modal.Header>
              <h2>Modal content</h2>
            </Modal.Header>

        </Modal>
     );
}

 
export default BookModal;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have run into an interesting problem -- when you click the X icon to close the modal, the <Card> component also receives this click!

So the onClick handler in the card code runs when you try to close the modal, and sets the open state to true again. This happens after the onClose handler in the modal is run, so the modal will stay open.

You can change the code to open modal on the <Image> click instead:

    <Card>
      <Image
        onClick={() => setShowModal(true)}
      >
        <Placeholder>
          <Image src={props.image} size="large" />
        </Placeholder>
      </Image>
      <Card.Content>
        <Card.Header>{props.title}</Card.Header>
        <Card.Meta>
          <span className="date">{props.publishedDate}</span>
        </Card.Meta>
        <Card.Description>{props.author}</Card.Description>
      </Card.Content>
      <Card.Content extra>
        <Icon name="star">Rating</Icon>
      </Card.Content>
      <BookModal
        key="Modal1"
        open={showModal}
        onClose={() => setShowModal(false)}
      />
    </Card>

You can see it all working here: https://codesandbox.io/s/semantic-ui-example-forked-yy7p3?file=/BookCard.js

Because the <Image> component is a sibling and not the parent of the <BookModal> component, the click will not "fall through" or "bubble up" (whatever semantics you prefer) into it.

Another way is to keep the modal trigger inside the modal. This is how their own examples do it -- you can find a simplified one here: https://codesandbox.io/s/semantic-ui-example-forked-qo99z?file=/example.js

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