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

119
Vistas
Modal state not being set to closed after history.push in React

I have a slider that contains "Events" cards, and when the user clicks on a card a modal shows up. And inside the modal, there's a button that redirects the user to a registration form.

I'm using react-modal.

The problem is that when the user clicks on the button, the scroll bar disappears. And my conclusion is that the modal state is still set as open after changing the current page.

Here is an extracted code of the News component and inside of it you'll find the modal:


import Modal from "react-modal";


function News() {
const [modalIsOpen, setIsOpen] = React.useState(false);

const closeModal = () => {
    setIsOpen(false);
  };
 return (
     <Button onClick={() => setIsOpen(true}>
       Open Modal
     </Button>

        <Modal
          className="modal-class"
          isOpen={modalIsOpen}
          onRequestClose={closeModal}
        >

        <EventDetail 
            onCloseModal={
                () => setIsOpen(false), 
                modalIsOpen
            }
        />

        </Modal>
 )
}

this is the EventDetail :

function EventDetail (onCloseModal) {

const navigateToRegistrationForm = () => {
    console.log(onCloseModal);
    history.push(`${baseUrl}/registrationForm/${form.id}`) ;
}
 return (

   <Button onClick={navigateToRegistrationForm}>
       Register
   </Button>

 )
}

The console.log returns "true" which means the modal is still set as open. How Can I change its state when the "navigateToRegistrationForm" function is called?

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

0

You were pretty close to achieving this. All you need is basically pass closeModal function down to EventDetail component like so:

<EventDetail onCloseModal={closeModal} /> and then call it inside navigateToRegistrationForm

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