react-use-geolocation

React hook for accessing geolocation.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
react-use-geolocation
0.1.15 years ago5 years agoMinified + gzip package size for react-use-geolocation in KB

Readme

react-use-geolocation
CircleCI Coverage Status
React hook for accessing geolocation.

Installation

Using npm:
$ npm install --save react-use-geolocation

Using yarn:
$ yarn add react-use-geolocation

Since this module uses React's new Hooks feature, to try this out you'll need to install the 16.8.0 version of react and react-dom:
$ yarn add react@^16.8.0 react-dom@^16.8.0

Usage

import { useCurrentPosition } from 'react-use-geolocation';
import React from 'react';

function Example() {
  const [position, error] = useCurrentPosition();

  if (!position && !error) {
    return <p>Waiting...</p>;
  }

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

  return (
    <div>
      <p>
        Latitude: {position.coords.latitude}
      </p>
      <p>
        Longitude: {position.coords.longitude}
      </p>
    </div>
  );
}

API

import { useCurrentPosition } from 'react-use-geolocation';

useCurrentPosition(options?: PositionOptions): [
  ?Position,
  ?PositionError
]

Identical to getCurrentPosition. Receives an optional PositionOptions and returns a tuple with the position and the error.
import { useWatchPosition } from 'react-use-geolocation';

useWatchPosition(options?: PositionOptions): [
  ?Position,
  ?PositionError
]

Same API as useCurrentPosition, but watches the location and updates the component when the location changes.

Contributing

Please feel free to submit any issues or pull requests.

License

MIT