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

145
Vistas
How to call a function everytime my route changes? using useEffect hook

I'm working on a chat application and I want to make it like whenever user opens a chat it should show the latest chat.

So to do that I made a function scrollToBottom() that scroll to the bottom of the chat after 1 second, but I want to make it run, everytime the route changes. I tried placing router.query.id in useEffect dependency but it runs on every second automatically.

 useEffect(() => {
    setTimeout(() => {
      scrollToBottom();
    }, 1000);
 }, [router.query.id]);

So basically I want this useEffect to run only when router.query.id changes, that is only when the URL changes. How can I do that? Help would be appreciated.

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

0

You can use next/Router's inbuilt events to detect route change.

const scroll = () => {
  setTimeout(() => {
    scrollToBottom();
  }, 1000);
};

useEffect(() => {
  Router.events.on('routeChangeStart', scroll)

  // Remember to remove event listeners on unmount
  return () => {   
    Router.events.off('routeChangeStart', scroll);
  } 

}, []);
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