useGeolocation
function useGeolocation(options?: GeolocationOptions): GeolocationState;
interface GeolocationOptions {
enabled?: boolean; // default true; false asks nothing yet
watch?: boolean; // keep following the position, default false
enableHighAccuracy?: boolean;
timeout?: number;
maximumAge?: number;
}
interface GeolocationState {
readonly status: "idle" | "pending" | "success" | "error" | "unsupported";
readonly position: GeolocationPosition | null;
readonly error: GeolocationPositionError | null; // code 1 is permission denied
}
Gets the user's position from the browser.
- It asks as soon as it mounts, and the browser may prompt the user. To ask only when the user
chooses, start with
enabled: false("idle") and enable it on a click. - With
watch, it follows the position as it changes, until disabled or unmounted. positionkeeps the last known position after a later error, and when disabled.- Changing the options asks again;
statusis"pending"until the new answer. - It is
"unsupported"without the Geolocation API, and"pending"during server rendering ("idle"when disabled).
Check usePermission("geolocation") first if you'd rather not prompt users
who already refused.
Example
import { useState } from "react";
import { useGeolocation } from "react-honey-hooks";
function NearbyStores() {
const [asked, setAsked] = useState(false);
const { status, position, error } = useGeolocation({ enabled: asked });
if (status === "idle") {
return (
<button
onClick={() => {
setAsked(true);
}}
>
Find stores near me
</button>
);
}
if (status === "error") return <p>Couldn't get your location: {error?.message}</p>;
if (!position) return <p>Locating…</p>;
return <StoreList near={position.coords} />;
}