Skip to content

Commit

Permalink
Merge pull request #53 from bence-toth/enable-option
Browse files Browse the repository at this point in the history
isEnabled option improvements
  • Loading branch information
bence-toth committed Aug 19, 2022
2 parents b4cb10f + eb45f87 commit 1e155b6
Show file tree
Hide file tree
Showing 5 changed files with 423 additions and 527 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ const ComponentWithGeolocation = () => {
};
```

### Disabling until the user has opted in

You can pass `false` as the third argument to prevent geolocation.

```jsx
const geolocation = useGeolocation({}, () => {}, false);
```

### Using `PositionOptions`

[There is a way](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API#Fine_tuning_response) to use `PositionOptions` to fine tune response coming from `watchPosition` of the Geolocation API.
Expand Down Expand Up @@ -77,6 +69,18 @@ const onGeolocationUpdate = (geolocation) => {
const geolocation = useGeolocation({}, onGeolocationUpdate);
```

### Waiting for the user to opt in

The best practice is to only try to geolocate the user on the user’s request, or at least when it is indicated to the user why the browser is asking for their location.

If you would like to bail out of this hook immediately asking for geolocation access from the user when the component mounts, you can pass a third `isEnabled` parameter to the hook with the value `false`, and set it to `true` later once the reason for asking for geolocation is indicated to the user.

```jsx
const geolocation = useGeolocation({}, () => {}, false);
```

Setting this parameter from `true` to `false` will not cut access to already retrieved geolocation data, it will however stop watching for changes in geolocation data until the parameter is set back to `true` again.

## Notes

Access to data from the Geolocation API needs user permission.
Expand Down
Loading

0 comments on commit 1e155b6

Please sign in to comment.