react-use-geolocation

0.1.1 • Public • Published

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

/react-use-geolocation/

    Package Sidebar

    Install

    npm i react-use-geolocation

    Weekly Downloads

    256

    Version

    0.1.1

    License

    MIT

    Unpacked Size

    8.59 kB

    Total Files

    6

    Last publish

    Collaborators

    • bsonntag