Skip to content

Commit 7b102d4

Browse files
committed
Add skip option to hook
1 parent 8aff56c commit 7b102d4

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export const Demo = () => {
120120
- `settings.enableHighAccuracy` - indicates the application would like to receive the most accurate results (default `false`),
121121
- `settings.timeout` - maximum length of time (in milliseconds) the device is allowed to take in order to return a position (default `Infinity`),
122122
- `settings.maximumAge` - the maximum age in milliseconds of a possible cached position that is acceptable to return (default `0`).
123+
- `skip: boolean` - set it to `true` to skip retrieving location.
123124

124125
### `usePosition()` output
125126

demo/Demo.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import {usePosition} from '../src/usePosition';
44

5-
export const Demo = ({watch, settings}) => {
5+
export const Demo = ({watch, settings, skip}) => {
66
const {
77
latitude,
88
longitude,
@@ -11,7 +11,7 @@ export const Demo = ({watch, settings}) => {
1111
speed,
1212
heading,
1313
error,
14-
} = usePosition(watch, settings);
14+
} = usePosition(watch, settings, skip);
1515

1616
const loader = !latitude && !error ? (
1717
<>
@@ -39,4 +39,5 @@ export const Demo = ({watch, settings}) => {
3939
Demo.propTypes = {
4040
watch: PropTypes.bool,
4141
settings: PropTypes.object,
42+
skip: PropTypes.bool,
4243
};

src/usePosition.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const defaultSettings = {
66
maximumAge: 0,
77
};
88

9-
export const usePosition = (watch = false, userSettings = {}) => {
9+
export const usePosition = (watch = false, userSettings = {}, skip = false) => {
1010
const settings = {
1111
...defaultSettings,
1212
...userSettings,
@@ -31,6 +31,10 @@ export const usePosition = (watch = false, userSettings = {}) => {
3131
};
3232

3333
useEffect(() => {
34+
if (skip) {
35+
return;
36+
}
37+
3438
if (!navigator || !navigator.geolocation) {
3539
setError('Geolocation is not supported');
3640
return;
@@ -44,9 +48,11 @@ export const usePosition = (watch = false, userSettings = {}) => {
4448

4549
navigator.geolocation.getCurrentPosition(onChange, onError, settings);
4650
}, [
51+
watch,
4752
settings.enableHighAccuracy,
4853
settings.timeout,
4954
settings.maximumAge,
55+
skip,
5056
]);
5157

5258
return {...position, error};

tests/__snapshots__/usePosition.test.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`usePosition should not call geolocation when skipped 1`] = `
4+
Array [
5+
<div>
6+
Trying to fetch location...
7+
</div>,
8+
<br />,
9+
<code>
10+
latitude:
11+
<br />
12+
longitude:
13+
<br />
14+
timestamp:
15+
<br />
16+
accuracy:
17+
<br />
18+
speed:
19+
<br />
20+
heading:
21+
<br />
22+
error:
23+
</code>,
24+
]
25+
`;
26+
327
exports[`usePosition should return empty values by default 1`] = `
428
Array [
529
<div>

tests/usePosition.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ describe('usePosition', () => {
124124
const tree = testRenderer.toJSON();
125125
expect(tree).toMatchSnapshot();
126126
});
127+
128+
it('should not call geolocation when skipped', () => {
129+
global.navigator.geolocation = {
130+
watchPosition: jest.fn(),
131+
getCurrentPosition: jest.fn(),
132+
clearWatch: jest.fn(),
133+
};
134+
135+
let testRenderer;
136+
act(() => {
137+
testRenderer = renderer.create(<Demo skip />);
138+
});
139+
const tree = testRenderer.toJSON();
140+
expect(tree).toMatchSnapshot();
141+
142+
expect(global.navigator.geolocation.watchPosition).not.toHaveBeenCalled();
143+
expect(global.navigator.geolocation.getCurrentPosition).not.toHaveBeenCalled();
144+
});
127145
});

0 commit comments

Comments
 (0)