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

142
Visualizações
Prompt users of unsaved changes if they try navigate to another page in ReactJS

I am working on a dynamic form where the form gets submitted automatically when all the required fields are filled. We don't have any submit button in the form. Now suppose a user has filled a few of the form fields and tries to leave the form by clicking any navigation link in the application, we need to warn the user with a pop up showing the message "You have unsaved changes. Navigating away from this view will revert these changes. This action cannot be undone. Are you sure you want to continue?". Now if the user clicks on the continue it will redirect to the navigated page and if he clicks on cancle it will stay on the same page(form).

Here we are using ReactJS but not using any react routing in the application. We have tried all possible ways but were not able to come up with any solution. Has somebody faced this scenario before or has any idea how to approach the problem?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use native browser API beforeUnload.

  ...
  const beforeunload = (e) => {
    // You can add logic for confirmation here.
    if (dataUnsaved) {
      e.preventDefault();
      e.returnValue = true;
    }
  }

  useEffect(() => {
    window.addEventListener('beforeunload', beforeunload);
    
    return () => {
        window.removeEventListener('beforeunload', beforeunload);
    }

  }, [])

  return (
   ...
  )
...
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