Skip to content

Instantly share code, notes, and snippets.

@itsMapleLeaf
Created August 13, 2019 12:20
Show Gist options
  • Select an option

  • Save itsMapleLeaf/54e3bc5e9e5efd8efafc65562050e4ba to your computer and use it in GitHub Desktop.

Select an option

Save itsMapleLeaf/54e3bc5e9e5efd8efafc65562050e4ba to your computer and use it in GitHub Desktop.
geolocation hook example
function SomeComponent() {
const { location, error } = useGeolocation()
useEffect(() => {
if (error) {
Analytics.trackEvent("LocationService: ", error.message, error.code)
handleLocationError(error)
}
}, [error])
// render something w/ location
}
function useGeolocation() {
const [location, setLocation] = useState()
const [error, setError] = useState()
useEffect(() => {
Geolocation.watchPosition((location) => {
setLocation(location)
setError(undefined) // clear error if getting location was a success
}, setError)
return () => {
// cleanup the watcher here
}
}, [])
return { location, error }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment