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

195
Vistas
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 Respuestas
Responde la pregunta

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 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