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

140
Vistas
CHange URL after a modal is closed

I am trying to change my URL after a modal is clicked.

I had added an extra onClick to the button which called the modal, this was to a function - in that function I added some console logging. I could see the logging, but the URL didn't change.

The (original) button code is:

<button className='buttonCheck' onClick={checkAnswer}>CHECK MY ANSWER</button> <CheckAnswerModal showModal={showCongratsModal} onClose={() => setShowCongratsModal(false)} videoMessage={showCongratsURL} content={showWindowContent} size='med'/>

And the modal

import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';

function CheckAnswerModal({showModal = false, onClose = () =>{}, videoMessage, content, size}) {

  return (
    <Modal
      size={size}
      show={showModal} 
      onHide={onClose}
      backdrop="static"
      keyboard={false}
    >

    <Modal.Body>
      {videoMessage ? <video src={videoMessage} controls autoPlay> </video>: <div><center>{content}</center></div>}
    </Modal.Body>
    <Modal.Footer>
      <Button variant="secondary" onClick={onClose}>
        Close
      </Button>
    </Modal.Footer>
  </Modal>
  )
}

export default CheckAnswerModal

Originally, I had added a new function to the calling page:

function GoHome() {
  console.log("redirecting")
  <Redirect to='/' />
}

and added this to the button onClick, I could see the logging, but no URL changing. I've done a bit of looking about and I am pretty sure this is not working as the modal is on screen. To get around this, in the GoHome() I even added a conditional (if the showModal is false, then do the logging)

I've seen some posts which talked about unmounting the component (now - this is something new to me, especially as I don't call any mount component explicitly.)

Am I missing something fundamental with redirect? Or can someone point me at what I am doing wrong (the redirect feels a little "hacky" just now, I need to redo a whole component I think, but this would work for now)

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

0

You have to install react-router-dom if you have not installed yet.

Then import it.

import {useNavigate} from react-router-dom

then call it inside a function like:

const navigate = useNavigate()

then in your onClose() function use navigate like:

navigate("/")
  • sorry for my English
about 4 years ago · Juan Pablo Isaza Denunciar

0

Rather than trying to be smart, I simply declared history at the start and then in my onClose added history.push and it worked.

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