-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |