Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda