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

225
Vistas
how to listen page url change without page refresh in react?

I do have some breadcrumbs on the page. When I do replaceState I see that url change there is no problem with that but I couldn't catch that url change in react component I've tried but no sense it works only once when component mounted.

const usePath = () => {
    const [path, pathSet] = useState(window.location.pathname);
    const listenToPopstate = () => {
        const winPath = window.location.pathname;
        pathSet(winPath);
    };
    useEffect(() => {
        window.addEventListener("locationchange", listenToPopstate);
        return () => {
            window.removeEventListener("locationchange", listenToPopstate);
        };
    }, []);
    return path;
};


const Breadcrumb = () => {

    const [state, stateSet] = useState(false);

    const path = usePath();

    useEffect(() => {
        path.indexOf('StoreSelection') > -1 && stateSet(true)
    }, [path]);

    return (
            <ol className="breadcrumb" style={breadcrumb}>
                <li key="1" className="breadcrumb-item align-items-center">
                    {state ? 'AAA' : 'BBB'}
                </li>
            </ol>
    );
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you'll find instantiating a history object and using it to listen will be easier for you.

history

import { createBrowserHistory } from 'history';

const history = createBrowserHistory();

const usePath = () => {
  const [path, pathSet] = useState(history.location.pathname);

  const listener = (location, action) => {
    pathSet(location.pathname);
  };

  useEffect(() => {
    const unlisten = history.listen(listener);
    return unlisten;
  }, []);

  return path;
};
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