Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ import useGeolocation from "react-hook-geolocation";
const ComponentWithGeolocation = () => {
const geolocation = useGeolocation();

return !geolocation.error ? (
if (geolocation.error) {
return <p>No geolocation, sorry.</p>;
}

if (geolocation.isLoading) {
return <p>Geolocation data is loading...</p>;
}

return (
<ul>
<li>Latitude: {geolocation.latitude}</li>
<li>Longitude: {geolocation.longitude}</li>
Expand All @@ -35,8 +43,6 @@ const ComponentWithGeolocation = () => {
<li>Speed: {geolocation.speed}</li>
<li>Timestamp: {geolocation.timestamp}</li>
</ul>
) : (
<p>No geolocation, sorry.</p>
);
};
```
Expand All @@ -59,12 +65,14 @@ const geolocation = useGeolocation({

You can supply a second parameter to `useGeolocation` which will be called every time the data from the Geolocation API is updated. This callback function is then called with the `geolocation` object with all its properties.

If you don't use `PositionOptions`, I recommend that you supply `{}` as your first argument.
To prevent unnecessary renders you can wrap your callback function in a `useCallback` hook to preserve its referential safety.

If you don’t use `PositionOptions`, I recommend that you supply `{}` as your first argument.

```jsx
const onGeolocationUpdate = (geolocation) => {
const onGeolocationUpdate = useCallback((geolocation) => {
console.log("Here’s some new data from the Geolocation API: ", geolocation);
};
}, []);

const geolocation = useGeolocation({}, onGeolocationUpdate);
```
Expand Down
Loading