TL;DR
In this article weâll create a React usePosition() hook to fetch and follow browserâs location. Under the hood weâll use getCurrentPosition and watchPosition functions that are provided by global object navigator.geolocation. The final version of the usePosition() hook is published on GitHub and NPM and is ready to be consumed by your apps.
Why we might need usePosition() hook at all
One of the advantages of React hooks is ability to separate concerns. Instead of having a state object with, letâs say, geo-position and socket connection we might avoid using state at all and just use two different hooks that will handle the state management for us. Even more, instead of initiating browser position watcher and opening a socket connection in the same componentDidMount() callback we might split this logic into two independent hooks. This gives us cleaner and more maintainable code.
How weâre going to usePosition() hook
Letâs do some reverse engineering and imagine that we already have a usePosition() hook implemented. Here is how we might want to use it:
â ïž All code samples below use wrong symbol =â» instead of => for lambda functions annotations. Be aware of this while copying the code and trying to execute it since it might not work. Please replace â» with > manually in your code. For some reason I had issues with pasting the > symbol inside the code block. Sorry for inconvenience.
import React from 'react';
import {usePosition} from './usePosition';
export const UsePositionDemo = () =â» {
const {latitude, longitude, error} = usePosition();
return (
<code>
latitude: {latitude}<br>
longitude: {longitude}<br>
error: {error}
</code>
);
};
You see, it is only one line with usePosition() hook and you already have the data (latitude and longitude). We donât even use useState() and useEffect() here. Position subscription and watcher cleanup is incapsulated in usePosition() hook. Now the redraw component magic will be handled for us by React and we will see the <code>âŠ</code> block constantly being updated with the latest position value of the browser. Looks pretty neat and clean.
usePosition() hook implementation
Our custom usePosition() hook is just a JavaScript function that uses other hooks like useState() and useEffect(). It will look something like:
// imports go here...
export const usePosition = () =â» {
// code goes here...
}
We will use useEffect() hook to hook to the moment in time when a component (that will consume our hook) is rendered and to subscribe to geolocation changes. We will also use useState() hook to store latitude, longitude and error message (in case if user wonât allow browser to share its position). So we need to import these hooks first:
import {useState, useEffect} from 'react';
export const usePosition = () =â» {
// code goes here...
}
Letâs init a storage for position and for error:
import {useState, useEffect} from 'react';
export const usePosition = () =â» {
const [position, setPosition] = useState({});
const [error, setError] = useState(null);
// other code goes here...
}
Letâs return a desirable values from the function. We donât have them yet but letâs return initial values so far and fill them later:
import {useState, useEffect} from 'react';
export const usePosition = () =â» {
const [position, setPosition] = useState({});
const [error, setError] = useState(null);
// other code goes here...
return {...position, error};
}
Here is a key part of our hook â fetching the browserâs position. We will execute fetching logic after component has been rendered (useEffect hook).Â
import {useState, useEffect} from 'react';
export const usePosition = () =â» {
const [position, setPosition] = useState({});
const [error, setError] = useState(null);
// callbacks will go here...
useEffect(() =â» {
const geo = navigator.geolocation;
if (!geo) {
setError('Geolocation is not supported');
return;
}
watcher = geo.watchPosition(onChange, onError);
return () =â» geo.clearWatch(watcher);
}, []);
return {...position, error};
}
In useEffect() hook we first do some checks to see if the browser is supporting navigator.geolocation. If geolocation is not supported weâre setting up an error and returning from the effect. In case if navigator.geolocation is supported we subscribe to position changes by providing an onChange() and onError() callbacks (weâll add them in a moment). Notice that weâre returning a lambda function from useEffect(). In that lambda function we are clearing the watcher once the component is unmounted. So this subscribe/unsubscribe logic will be handled internally by our usePosition() hook and consumers shouldnât worry about it.
Letâs now add missing callbacks:
import {useState, useEffect} from 'react';
export const usePosition = () =â» {
const [position, setPosition] = useState({});
const [error, setError] = useState(null);
const onChange = ({coords}) =â» {
setPosition({
latitude: coords.latitude,
longitude: coords.longitude,
});
};
const onError = (error) =â» {
setError(error.message);
};
useEffect(() =â» {
const geo = navigator.geolocation;
if (!geo) {
setError('Geolocation is not supported');
return;
}
watcher = geo.watchPosition(onChange, onError);
return () =â» geo.clearWatch(watcher);
}, []);
return {...position, error};
}
And weâre done. The hook usePosition() may be consumed and it incapsulates only geolocation related logic.
Afterword
You may find a demo and more detailed implementation of usePosition() hook on GitHub. I hope this example was informative for you. Happy coding!
Top comments (2)
I prefer the name
useGeolocation
tousePosition
.Always prefer explicit to implicit.
This is great! Thanks a lot for sharing.