Location Provider
A context provider for managing location data and formatting across your application.
API reference
LocationProvider
The main provider component that wraps your application to provide location functionality.
| Option | Description | Default |
|---|---|---|
children | Type: ReactNode (required)The child components that will have access to the location context. | |
coordinates | Type: Coordinates | null (optional)Current coordinates value. Can be used as an initial value or as a controlled value; pass null to clear. | |
formatter | Type: CoordinatesFormatter (optional)Custom formatter function for coordinates. | |
onChange | Type: (coordinates: Coordinates | null) => void (optional)Called whenever coordinates change, including when they are cleared (null). | |
useLocationContext
A custom hook to access the location context within child components.
Returns an object with the following properties:
| Name | Type | Description |
|---|---|---|
coordinates | Coordinates | null | The current coordinates, or null when unset. |
formattedCoordinates | FormattedCoordinates | null | Structured formatted coordinates, or null if there are no coordinates or no formatter. |
coordinatesString | string | null | The coordinates as a formatted string, or null when unset. |
setCoordinates | (newCoordinates: Coordinates | null) => void | Updates the coordinates; pass null to clear the current selection. |
Types
Coordinates
The structure of the coordinates object.
interface Coordinates {
lat: number;
lon: number;
}FormattedCoordinates
The structure of the formatted coordinates object.
interface FormattedCoordinates {
lat: string | number;
lon: string | number;
}CoordinatesFormatter
The type for the custom formatter function.
type CoordinatesFormatter = (coordinates: Coordinates) => FormattedCoordinates;