Skip to content

useGeolocation

Track the current geolocation of the client using this hook.

Options

Return Values

NameTypeDescription
isLoadingBooleana loading state.
errorObjectthe gerolocation error if the page doesn't have the permission for example, or other things.
...coordinatesObjectthe current geolocation coordinates.

Example Usage

tsx
import { useGeolocation } from 'react-pre-hooks';

export default function Example() {
  const { isLoading, error, ...coords } = useGeolocation();

  if (isLoading) return <div>Loading...</div>;

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

  return (
    <main>
      <table>
        {Object.entries(coords).map(([name, value]) => (
          <tr>
            <th>{name}</th>
            <td>{value}</td>
          </tr>
        ))}
      </table>
    </main>
  );
}
import { useGeolocation } from 'react-pre-hooks';

export default function Example() {
  const { isLoading, error, ...coords } = useGeolocation();

  if (isLoading) return <div>Loading...</div>;

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

  return (
    <main>
      <table>
        {Object.entries(coords).map(([name, value]) => (
          <tr>
            <th>{name}</th>
            <td>{value}</td>
          </tr>
        ))}
      </table>
    </main>
  );
}

Released under the MIT License.