useLocation() returns a location object with properties like pathname, the URL path, e.g. '/products/42', search, the raw query string, e.g. '?sort=price', and hash, the URL fragment after #, reflecting the current URL at any point in a component tree rendered within a Router. It's especially useful for logic that needs to react to the current route without necessarily being the specific component that matched that route via Route, such as tracking page views for analytics, conditionally highlighting the active navigation link, or reading query parameters that aren't part of a route's own path pattern.
1Understanding useLocation
useLocation() returns a location object with properties like pathname, the URL path, e.g. '/products/42', search, the raw query string, e.g. '?sort=price', and hash, the URL fragment after #, reflecting the current URL at any point in a component tree rendered within a Router. It's especially useful for logic that needs to react to the current route without necessarily being the specific component that matched that route via Route, such as tracking page views for analytics, conditionally highlighting the active navigation link, or reading query parameters that aren't part of a route's own path pattern.
location.search gives you the raw query string, like '?sort=price&page=2' — pass it to the URLSearchParams constructor, new URLSearchParams(location.search), to conveniently parse individual query parameters rather than manually splitting the string yourself.
import { useLocation } from 'react-router-dom';
function CurrentPath() {
const location = useLocation();
return <p>You are at: {location.pathname}</p>;
}
// Visiting /products/42 renders:2Practical Example
Here is a real-world application of useLocation showing how it is used in production React code.
import { useLocation } from 'react-router-dom';
function SearchResults() {
const location = useLocation();
const params = new URLSearchParams(location.search);
return <p>Sorting by: {params.get('sort')}</p>;
}
// Visiting /search?sort=price renders:3Best Practices
Follow these guidelines when working with useLocation:
1. Use useLocation() to read the current pathname for logic like highlighting an active navigation link, comparing it against each link's own target path
2. Parse location.search with new URLSearchParams(location.search) to cleanly extract individual query parameters, rather than manually splitting the raw string
3. Include location.pathname, or the full location object, in a useEffect's dependency array when an effect, like an analytics page-view log, needs to re-run specifically when the route changes
Tip: location.search gives you the raw query string, like '?sort=price&page=2' — pass it to the URLSearchParams constructor, new URLSearchParams(location.search), to conveniently parse individual query parameters rather than manually splitting the string yourself.
import { useLocation } from 'react-router-dom';
function CurrentPath() {
const location = useLocation();
return <p>You are at: {location.pathname}</p>;
}
// Visiting /products/42 renders: