useLocationdeclare function useLocation(): Location;
interface Location<State = any> extends Path {
state: State;
key: string;
}
interface Path {
pathname: string;
search: string;
hash: string;
}
This hook returns the current location object. This can be useful if you'd like to perform some side effect whenever the current location changes.
import * as React from 'react';
import { useLocation } from 'react-router-dom';
function App() {
let location = useLocation();
React.useEffect(() => {
// Google Analytics
ga('send', 'pageview');
}, [location]);
return (
// ...
);
}
location.hashThe hash of the current URL.
location.keyThe unique key of this location.
location.pathnameThe path of the current URL.
location.searchThe query string of the current URL.
location.stateThe state value of the location created by <Link state> or navigate.