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

199
Visualizações
How to detect if the user is closing the browser OR navigate on other website

I want to implement session handling in the way that if the user closes the browser or tab all sessions are expired/destroyed. But I want to keep all the sessions if the user is navigating to another website. How can I do this in Nextjs?

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

0

You can use an event listener for an unload event: https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event

But given that you're in React/Next.js land, you'll likely want to hook up that listener inside a useEffect hook, and if you're going to do that ...

... you can simply skip the event listener entirely, and use the return value of the useEffect as your "unload" handler.

As explained here (https://reactjs.org/docs/hooks-effect.html) when you return a function from inside a useEffect, that function gets called when the component is removed from the page.

If you add the code you want to trigger "onUnload" into the returned function from a useEffect on your "App" component, this will have the same effect as using an unload event listener.

// App.js
import { useEffect}, React from 'react'

App = () => {
   useEffect(() => {
       return () => {
         // add code to trigger when the user leaves here
       }
   })
}
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