Skip to content

Commit f99c89f

Browse files
committed
feat: 🎸 add useLifecycles hook
1 parent 2541716 commit f99c89f

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
- [`useTitle`](./docs/useTitle.md) — sets title of the page.
6060
<br/>
6161
<br/>
62+
- [__Lifecycles__](./docs/Lifecycles.md)
63+
- [`useLifecycles`](./docs/useLifecycles.md) &mdash; calls `mount` and `unmount` callbacks.
64+
<br/>
65+
<br/>
6266
- [__State__](./docs/State.md)
6367
- [`useToggle`](./docs/useToggle.md) &mdash; tracks state of a boolean.
6468
- [`useCounter`](./docs/useCounter.md) &mdash; tracks state of a number.

docs/useLifecycles.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `useLifecycles`
2+
3+
React lifecycle hook that call `mount` and `unmount` callbacks, when
4+
component is mounted and un-mounted, respectively.
5+
6+
7+
## Usage
8+
9+
```jsx
10+
import {useLifecycles} from 'react-use';
11+
12+
const Demo = () => {
13+
useLifecycles(() => console.log('MOUNTED'), () => console.log('UNMOUNTED'));
14+
return null;
15+
};
16+
```
17+
18+
19+
## Reference
20+
21+
```js
22+
useLifecycles(mount, unmount);
23+
```
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {storiesOf} from '@storybook/react';
2+
import * as React from 'react';
3+
import {useLifecycles} from '..';
4+
5+
const Demo = () => {
6+
useLifecycles(() => console.log('MOUNTED'), () => console.log('UNMOUNTED'));
7+
return null;
8+
};
9+
10+
storiesOf('useLifecycles', module)
11+
.add('Example', () =>
12+
<Demo/>
13+
)

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import useFavicon from './useFavicon';
66
import useGeolocation from './useGeolocation';
77
import useHover from './useHover';
88
import useIdle from './useIdle';
9+
import useLifecycles from './useLifecycles';
910
import useList from './useList';
1011
import useLocalStorage from './useLocalStorage';
1112
import useLocation from './useLocation';
@@ -33,6 +34,7 @@ export {
3334
useGeolocation,
3435
useHover,
3536
useIdle,
37+
useLifecycles,
3638
useList,
3739
useLocalStorage,
3840
useLocation,

src/useIdle.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ const useIdle = (ms: number = oneMinute, initialState: boolean = false, events:
99
const [state, setState] = useState<boolean>(initialState);
1010

1111
useEffect(() => {
12+
let mounted = true;
1213
let timeout: any;
1314
let localState: boolean = state;
1415
const set = (newState: boolean) => {
15-
localState = newState;
16-
setState(newState);
16+
if (mounted) {
17+
localState = newState;
18+
setState(newState);
19+
}
1720
};
1821

1922
const onEvent = throttle(50, () => {
@@ -36,6 +39,8 @@ const useIdle = (ms: number = oneMinute, initialState: boolean = false, events:
3639
timeout = setTimeout(() => set(true), ms);
3740

3841
return () => {
42+
mounted = false;
43+
3944
for (let i = 0; i < events.length; i++) {
4045
off(window, events[i], onEvent);
4146
}

src/useLifecycles.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {useEffect} from './react';
2+
3+
const useLifecycles = (mount, unmount) => {
4+
useEffect(() => {
5+
if (mount) mount();
6+
return () => {
7+
if (unmount) unmount();
8+
};
9+
}, []);
10+
};
11+
12+
export default useLifecycles;

0 commit comments

Comments
 (0)