Skip to main content

useLocation

Returns the current Location 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

object
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:
Or with navigate:
Receiving state:

Conditional rendering based on URL

Show background location for modals

Type Safety

You can type the location state for better type checking: