logoReact Modern Hooks

Documentation

Getting Started

Installation

Hooks Overview

useFetch( )useCopyToClipboard( )useNetwork( )useFullScreen( )useGeolocation( )useSelectedText( )useStateCallback( )useResize( )useSearch( )useImageDownload( )

useGeolocation( )

A react hook which gets a users/agent's current geographic location. It return both the city, ip, country and other location variables useful in determining a users position on the world map

Usage

1import { useGeolocation } from 'react-modern-hooks'
2
3const Demo = () => {
4 const {
5 loading,
6 error,
7 userIP,
8 city,
9 country,
10 latitude,
11 longitude,
12 region,
13 location //Object that comes with other properties on top
14 } = useGeolocation()
15
16 if (error) {
17 console.log('Error: ', error)
18 }
19
20 if (loading) return <p>Loading...</p>
21
22 return (
23 <div>
24 <p>User IP Address: {userIP}</p>
25 <p>City: {city}</p>
26 <p>Country: {country}</p>
27 <p>Region: {region}</p>
28 <p>Longitude: {longitude}</p>
29 <p>Latitude: {latitude}</p>
30 <p>Location Timezone: {location?.timezone}</p>
31 </div>
32 )
33}
34
35export default Demo

References

Input Variables

No input variables

Output Variables

location - Location object with all other user details

latitude - Current latitude of a user/agent's position

longitude - Current longitude of a user/agent's position

userIP - Current IP address of a user/agent

city - City the user/agent is currently located in

region - Region the user/agent is currently located in

country - Country the user/agent is currently located in

error - Any error occurring during geolocating a user/agent

loading - A boolean variable indicating if a users location is still being fetched