one-hook
Size
0.38 kb
View source

useGeolocation

Access and watch the browser's geolocation data.

Installation

npm install @1hook/use-geolocation

Usage

import {  } from '@1hook/use-geolocation'

When mounted, this hook begins tracking the user's location.
If location permissions haven't been granted yet, the browser will request permission from the user.

function () {
  const { , ,  } = ()

  if ( === 'loading') {
    return <>Loading location...</>
  }

  if ( === 'error') {
    return <>Error: {.}</>
  }

  return (
    <>
      <>Latitude: {.}</>
      <>Longitude: {.}</>
    </>
  )
}

API Reference

UseGeolocationOptions

Options for the useGeolocation hook. See MDN docs  for more details.

PropTypeDefault
maximumAge?
number
-
timeout?
number
-
enableHighAccuracy?
boolean
-

UseGeolocationReturn

The return value of the useGeolocation hook.

PropTypeDefault
coords
GeolocationCoordinates & { timestamp: number | null }
-
error
GeolocationPositionError | null
-
state
"loading" | "watching" | "error"
-

On this page