Skip to content

Commit

Permalink
feat: 🎸 add useGeolocation hook
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Oct 27, 2018
1 parent 406af20 commit 13a7326
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [`useMap`](./docs/useMap.md)
- Sensors
- [`useBattery`](./docs/useBattery.md)
- [`useGeolocation`](./docs/useGeolocation.md)
- [`useHover`](./docs/useHover.md)
- [`useSize`](./docs/useSize.md)
- [`useWindowSize`](./docs/useWindowSize.md)
Expand Down
8 changes: 3 additions & 5 deletions docs/useBattery.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ const Demo = () => {
const state = useBattery();

return (
<div>
<pre>
{JSON.stringify(state, null, 2)}
</pre>
</div>
<pre>
{JSON.stringify(state, null, 2)}
</pre>
);
};
```
20 changes: 20 additions & 0 deletions docs/useGeolocation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `useGeolocation`

React sensor hook that tracks user's geographic location.


## Usage

```jsx
import {useGeolocation} from 'react-use';

const Demo = () => {
const state = useGeolocation();

return (
<pre>
{JSON.stringify(state, null, 2)}
</pre>
);
};
```
8 changes: 3 additions & 5 deletions src/__stories__/useBattery.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ const Demo = () => {
const state = useBattery();

return (
<div>
<pre>
{JSON.stringify(state, null, 2)}
</pre>
</div>
<pre>
{JSON.stringify(state, null, 2)}
</pre>
);
};

Expand Down
18 changes: 18 additions & 0 deletions src/__stories__/useGeolocation.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as React from 'react';
import {storiesOf} from '@storybook/react';
import {useGeolocation} from '..';

const Demo = () => {
const state = useGeolocation();

return (
<pre>
{JSON.stringify(state, null, 2)}
</pre>
);
};

storiesOf('useGeolocation', module)
.add('Example', () =>
<Demo/>
)
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import useBattery from './useBattery';
import useCounter from './useCounter';
import useGeolocation from './useGeolocation';
import useHover from './useHover';
import useList from './useList';
import useMap from './useMap';
Expand All @@ -11,6 +12,7 @@ import useWindowSize from './useWindowSize';
export {
useBattery,
useCounter,
useGeolocation,
useHover,
useList,
useMap,
Expand Down
56 changes: 56 additions & 0 deletions src/useGeolocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {useState, useEffect} from './react';

export interface GeoLocationSensorState {
accuracy: number,
altitude: number,
altitudeAccuracy: number,
heading: number,
latitude: number,
longitude: number,
speed: number,
timestamp: number
}

const useGeolocation = () => {
const [state, setState] = useState({
accuracy: null,
altitude: null,
altitudeAccuracy: null,
heading: null,
latitude: null,
longitude: null,
speed: null,
timestamp: Date.now(),
});
let mounted = true;
let watchId: any;

const onEvent = (event: any) => {
if (mounted) {
setState({
accuracy: event.coords.accuracy,
altitude: event.coords.altitude,
altitudeAccuracy: event.coords.altitudeAccuracy,
heading: event.coords.heading,
latitude: event.coords.latitude,
longitude: event.coords.longitude,
speed: event.coords.speed,
timestamp: event.timestamp,
});
}
};

useEffect(() => {
navigator.geolocation.getCurrentPosition(onEvent);
watchId = navigator.geolocation.watchPosition(onEvent);

return () => {
mounted = false;
navigator.geolocation.clearWatch(watchId);
};
}, [0]);

return state;
};

export default useGeolocation;

0 comments on commit 13a7326

Please sign in to comment.