Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isEnabled option improvements #53

Merged
merged 10 commits into from
Aug 19, 2022
Merged
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: 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