Hone logo
Hone
Problems

React Geolocation Hook: useGeolocation

Building a useGeolocation hook allows your React applications to access the user's geographical location. This is incredibly useful for location-based services, mapping applications, personalized content delivery, and more. This challenge asks you to create a robust and reusable hook that handles geolocation requests and provides location data to your components.

Problem Description

You need to create a custom React hook called useGeolocation. This hook should:

  1. Request Geolocation: Upon mounting, the hook should attempt to retrieve the user's location using the browser's Geolocation API.
  2. Handle Permissions: The hook must gracefully handle cases where the user denies location permission.
  3. Provide Location Data: The hook should return an object containing the following properties:
    • latitude: The latitude of the user's location (number or null if unavailable).
    • longitude: The longitude of the user's location (number or null if unavailable).
    • accuracy: The accuracy of the location data in meters (number or null if unavailable).
    • error: An error object if an error occurred during geolocation (Error object or null).
    • isLoaded: A boolean indicating whether the location data has been loaded (true/false).
    • isLoading: A boolean indicating whether the geolocation request is in progress (true/false).
  4. Update on Changes: If the user's location changes (e.g., through a watchPosition update), the hook should update the latitude, longitude, and accuracy values accordingly.
  5. Cleanup: The hook should properly clean up the geolocation watchPosition when the component unmounts to prevent memory leaks.

Expected Behavior:

  • On initial mount, isLoading should be true, and isLoaded, latitude, longitude, accuracy, and error should be false, null, null, null, and null respectively.
  • Upon successful geolocation retrieval, isLoading should become false, isLoaded should become true, and latitude, longitude, and accuracy should be populated with the retrieved values. error should be null.
  • If the user denies permission, isLoading should become false, isLoaded should become true, latitude, longitude, and accuracy should be null, and error should be populated with a relevant error object.
  • If an error occurs during geolocation, isLoading should become false, isLoaded should become true, latitude, longitude, and accuracy should be null, and error should be populated with the error object.
  • When the component unmounts, the watchPosition should be cleared.

Examples

Example 1:

Input: User grants location permission and the browser successfully retrieves location data.
Output: { latitude: 34.0522, longitude: -118.2437, accuracy: 10, error: null, isLoaded: true, isLoading: false }
Explanation: The hook successfully retrieved the location and updated the state.

Example 2:

Input: User denies location permission.
Output: { latitude: null, longitude: null, accuracy: null, error: PermissionDeniedError, isLoaded: true, isLoading: false }
Explanation: The hook detected that the user denied permission and updated the state accordingly.

Example 3:

Input: Browser fails to retrieve location data due to a network error.
Output: { latitude: null, longitude: null, accuracy: null, error: GeolocationError("Position unavailable"), isLoaded: true, isLoading: false }
Explanation: The hook detected a geolocation error and updated the state with the error object.

Constraints

  • The hook must be written in TypeScript.
  • The hook should use the standard browser Geolocation API.
  • The hook should handle potential errors gracefully.
  • The hook should avoid memory leaks by properly cleaning up the watchPosition when the component unmounts.
  • The hook should be compatible with modern React versions (16.8+).

Notes

  • Consider using useEffect to manage the lifecycle of the geolocation request.
  • The GeolocationPosition and GeolocationError interfaces are available in the browser's Geolocation API.
  • Think about how to handle the case where the user's location changes while the component is mounted. watchPosition is key here.
  • Error handling is crucial. The Geolocation API can throw various errors, so make sure your hook handles them appropriately.
  • Remember to return a stable object from the hook. Avoid creating new objects on every render unless necessary.
Loading editor...
typescript