@clurts/use-location
v2.2.0
Published
custom react hook to use the geolocation api
Readme
use-location
Installation
To install using npm:
npm i @clurts/use-locationTo install using yarn:
yarn add @clurts/use-locationUsage
Import the useLocation hook:
import useLocation from "@clurts/use-location"In your component use as follows:
const [isLocating, position, error] = useLocation()Api
isLocating: boolean - indicates wehter or not your postion is being retrieved currently.
position: object - containing geolocation information
Properties on the position object: | Property | Description | | --- | ----------- | | lat | Returns the current position latitude | | long | Returns the current position longitude |
error: string - error messages if access to location data is blocked or geolocation is not supported by the browser.
TypeScript Support
This package provides full TypeScript type definitions out of the box. When used in TypeScript projects, types are automatically inferred:
import useLocation, { Location, LocationResult } from "@clurts/use-location";
const [isLocating, position, error]: LocationResult = useLocation();
// position: LocationLocationtype:type Location = { lat: number | null; long: number | null; };LocationResulttype:type LocationResult = [boolean, Location, string | null];
No additional configuration is needed. Type definitions are included in the published package.
