Install

npm
npm i use-any-hook

Description

The useGeoLocation hook provides a simple way to retrieve the user’s current geographic location. This is useful for applications that need to display or use location-based data, such as maps, weather information, or location-based services. The hook handles the process of fetching the location and provides both the location data and any potential errors.

Parameters

This hook does not require any parameters.

Return Values:

NameTypeDescription
locationobjectThe location object containing geographic coordinates (latitude and longitude) and other metadata.
errorobjectAn error object if there was an issue retrieving the location, or null if no errors occurred.

Example

useGeoLocation.example.tsx
import { useGeoLocation } from "use-any-hook";

function MyComponent() {
  const { location, error } = useGeoLocation();

  if (error) {
    return <div>Error: {error.message}</div>;
  }

  return (
    <div>
      {location ? (
        <div>
          Latitude: {location.coords.latitude}, Longitude:{" "}
          {location.coords.longitude}
        </div>
      ) : (
        <div>Fetching location...</div>
      )}
    </div>
  );
}