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

218
Visualizações
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 Respostas
Responde à pergunta

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