Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!