
import React, { useEffect } from 'react';


/**
 * Hook will listen to location change and trigger the listener
 * 
 * @returns 
 */
export default function useLocation() {

    const [path, setPath] = React.useState(window.location.pathname);

    const listenToPopstate = () => {

        const winPath = window.location.pathname;
        
        setPath(winPath);
    };

    useEffect(() => {

        window.addEventListener("popstate", listenToPopstate);

        return () => {
            window.removeEventListener("popstate", listenToPopstate);
        };

    }, []);

    return path;

};
