@reacthooks.org/use-watch-position

1.0.2 • Public • Published

Install

$ npm install @reacthooks.org/use-watch-position

useWatchPosition

A React Hook (using useEffect) which uses the Geolocation API, specifically navigator.geolocation.watchPosition().

Synopsis

In your React component:

import { useState } from 'react'
import useSetTimeout from "use-set-timeout"

function SetTimeout() {
  const [ position, setPosition ] = useState(false)

  useWatchPosition((err, newPosition) => {
    if (err) {
      // either the user refused access, or something bad happened
      // * err.code === 1 // Permission Denied
      // * err.code === 2 // Position Unavailable
      // * err.code === 3 // Timeout
      // * no err.code    // some other error!
    }
    setPosition(newPosition)
  })

  return (
    <p>latitude = { position && position.coords && position.coords.latitude }</p>
    <p>longitude = { position && position.coords && position.coords.logitude }</p>
  )
}

Other Hooks

Please see all of the other reacthooks.org hooks:

Author

$ npx chilts

   ╒════════════════════════════════════════════════════╕
   │                                                    │
   │   Andrew Chilton (Personal)                        │
   │   -------------------------                        │
   │                                                    │
   │          Email : andychilton@gmail.com             │
   │            Web : https://chilts.org                │
   │        Twitter : https://twitter.com/andychilton   │
   │         GitHub : https://github.com/chilts         │
   │         GitLab : https://gitlab.org/chilts         │
   │                                                    │
   │   Apps Attic Ltd (My Company)                      │
   │   ---------------------------                      │
   │                                                    │
   │          Email : chilts@appsattic.com              │
   │            Web : https://appsattic.com             │
   │        Twitter : https://twitter.com/AppsAttic     │
   │         GitLab : https://gitlab.com/appsattic      │
   │                                                    │
   │   Node.js / npm                                    │
   │   -------------                                    │
   │                                                    │
   │        Profile : https://www.npmjs.com/~chilts     │
   │           Card : $ npx chilts                      │
   │                                                    │
   ╘════════════════════════════════════════════════════╛

(Ends)

Package Sidebar

Install

npm i @reacthooks.org/use-watch-position

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

8.41 kB

Total Files

5

Last publish

Collaborators

  • chilts
  • chilts-appsattic