useLocation
Returns the currentLocation object, which represents the current URL. This can be useful if you’d like to perform some side effect whenever it changes.
Signature
Parameters
None.Returns
The current location object with the following properties:
Usage
Track page views
Access pathname
Access search params
For working with search params, consider using
useSearchParams instead.Access hash
Access location state
Location state is useful for passing data between routes without including it in the URL. Sending state:navigate:
Conditional rendering based on URL
Show background location for modals
Type Safety
You can type the location state for better type checking:Related
useNavigate- Navigate programmaticallyuseSearchParams- Work with search paramsuseParams- Access URL parameters<Link>- Navigation with state