Skip to main content

useGeolocation

What is it needed for#

This hook is designed to work comfortably with the Geolocation API in the browser. It provides an easier way to get location data.

Advantages#

Using this hook allows you to get rid of a large amount of boilerplate code, get ready-made data about geolocation, the state of loading of this data, as well as errors that have occurred.

How to start using#

To start using this hook, you need to import it into your project:

import React from 'react';import { useGeolocation } from 'react-hooks-kit';
const Component = () => {   const { loading, error, position } = useGeolocation({     enableHightAccuracy: true   });
  if (loading) {    return <LoaderComponent />;  }
  if (error) {    return <ErrorComponent />  }
  return <div>    <h2>Data:</h2>    <p>{position.coords.latitude}</p>    <p>{position.coords.longtitude}</p>  </div>};

The options parameter is optional.

import React from 'react';import { useGeolocation } from 'react-hooks-kit';
const Component = () => {  const { loading, error, position } = useGeolocation();
  if (loading) {    return <LoaderComponent />;  }
  if (error) {    return <ErrorComponent />  }
  return <div>    <h2>Data:</h2>    <p>{position.coords.latitude}</p>    <p>{position.coords.longtitude}</p>  </div>};

The following functions are available for operation:

Options#

NameTypeDescription
loadingbooleanReturns the loading status when requesting geolocation.
errorPositionError, undefinedReturns the error status when requesting geolocation.
positionPosition, undefinedReturns geolocation data.