Skip to content

Commit

Permalink
feat: 🎸 add useRefMounted hook
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 5, 2018
1 parent f755001 commit dfb0510
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<br/>
- [**Lifecycles**](./docs/Lifecycles.md)
- [`useLifecycles`](./docs/useLifecycles.md) &mdash; calls `mount` and `unmount` callbacks.
- [`useRefMounted`](./docs/useRefMounted.md) &mdash; tracks if component is mounted.
- [`useLogger`](./docs/useLogger.md) &mdash; logs in console as component goes though life-cycles.
- [`useMount`](./docs/useMount.md) &mdash; calls `mount` callbacks.
- [`useUnmount`](./docs/useUnmount.md) &mdash; calls `unmount` callbacks.
Expand Down
25 changes: 25 additions & 0 deletions docs/useRefMounted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# `useRefMounted`

Lifecycle hook that tracks if component is mounted. Returns a ref, which has a
boolean `.current` property.


## Usage

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

const Demo = () => {
const refMounted = useRefMounted();

useEffect(() => {
setTimeout(() => {
if (refMounted.currrent) {
// ...
} else {
// ...
}
}, 1000);
});
};
```
20 changes: 20 additions & 0 deletions src/__stories__/useRefMounted.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {storiesOf} from '@storybook/react';
import * as React from 'react';
import {useRefMounted, useRaf} from '..';
import ShowDocs from '../util/ShowDocs';

const Demo = () => {
const refMounted = useRefMounted();
useRaf();
return (
<div>
is mounted: {refMounted.current ? '👍' : '👎'}
</div>
);
};

storiesOf('useRefMounted', module)
.add('Docs', () => <ShowDocs md={require('../../docs/useRefMounted.md')} />)
.add('Demo', () =>
<Demo/>
)
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import useObservable from './useObservable';
import useOrientation from './useOrientation';
import useOutsideClick from './useOutsideClick';
import useRaf from './useRaf';
import useRefMounted from './useRefMounted';
import useRenderProp from './useRenderProp';
import useSessionStorage from './useSessionStorage';
import useSetState from './useSetState';
Expand Down Expand Up @@ -79,6 +80,7 @@ export {
useOrientation,
useOutsideClick,
useRaf,
useRefMounted,
useRenderProp,
useSessionStorage,
useSetState,
Expand Down
12 changes: 12 additions & 0 deletions src/useRefMounted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {useRef, useEffect} from 'react';

const useRefMounted = () => {
const refMounted = useRef(false);
useEffect(() => {
refMounted.current = true;
return () => refMounted.current = false;
});
return refMounted;
};

export default useRefMounted;

0 comments on commit dfb0510

Please sign in to comment.