Skip to content

Commit

Permalink
feat: add useEffectOnce hook
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich authored Mar 31, 2019
2 parents 08f796a + d2bb09d commit 06c12d4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<br/>
<br/>
- [**Lifecycles**](./docs/Lifecycles.md)
- [`useEffectOnce`](./docs/useEffectOnce.md) &mdash; a modified [`useEffect`](https://reactjs.org/docs/hooks-reference.html#useeffect) hook that only runs once.
- [`useEvent`](./docs/useEvent.md) &mdash; subscribe to events.
- [`useLifecycles`](./docs/useLifecycles.md) &mdash; calls `mount` and `unmount` callbacks.
- [`useRefMounted`](./docs/useRefMounted.md) &mdash; tracks if component is mounted.
Expand Down
27 changes: 27 additions & 0 deletions docs/useEffectOnce.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# `useEffectOnce`

React lifecycle hook that runs an effect only once.

## Usage

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

const Demo = () => {
useEffectOnce(() => {
console.log('Running effect once on mount')

return () => {
console.log('Running clean-up of effect on unmount')
}
});

return null;
};
```

## Reference

```js
useEffectOnce(effect: EffectCallback);
```
21 changes: 21 additions & 0 deletions src/__stories__/useEffectOnce.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {storiesOf} from '@storybook/react';
import * as React from 'react';
import {useEffectOnce} from '..';
import ConsoleStory from './util/ConsoleStory'
import ShowDocs from '../util/ShowDocs';

const Demo = () => {
useEffectOnce(() => {
console.log('Running effect once on mount')

return () => {
console.log('Running clean-up of effect on unmount')
}
});

return <ConsoleStory />;
};

storiesOf('Lifecycles|useEffectOnce', module)
.add('Docs', () => <ShowDocs md={require('../../docs/useEffectOnce.md')} />)
.add('Demo', () => <Demo/>)
7 changes: 7 additions & 0 deletions src/__stories__/util/ConsoleStory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

const ConsoleStory = ({message = 'Open the developer console to see logs'}) => (
<p>{message}</p>
);

export default ConsoleStory
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useDropArea from './useDropArea';
import useCounter from './useCounter';
import useCss from './useCss';
import useDebounce from './useDebounce';
import useEffectOnce from './useEffectOnce';
import useEvent from './useEvent';
import useFavicon from './useFavicon';
import useFullscreen from './useFullscreen';
Expand Down Expand Up @@ -77,6 +78,7 @@ export {
useCounter,
useCss,
useDebounce,
useEffectOnce,
useEvent,
useFavicon,
useFullscreen,
Expand Down
7 changes: 7 additions & 0 deletions src/useEffectOnce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {useEffect, EffectCallback} from 'react';

const useEffectOnce = (effect: EffectCallback) => {
useEffect(effect, []);
}

export default useEffectOnce;

0 comments on commit 06c12d4

Please sign in to comment.