Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/CanopyTax/use-once
A helper for running react effects only once
https://github.com/CanopyTax/use-once
Last synced: 3 months ago
JSON representation
A helper for running react effects only once
- Host: GitHub
- URL: https://github.com/CanopyTax/use-once
- Owner: CanopyTax
- License: mit
- Created: 2018-10-29T18:25:57.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-29T18:46:03.000Z (about 6 years ago)
- Last Synced: 2024-04-24T22:20:27.535Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 9
- Watchers: 12
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome - use-once - A helper for running react effects only once (JavaScript)
README
# use-once
A helper for running react effects only once.## Motivation
For beginners, it isn't clear that `useEffect(cbk, [])` [runs only once](https://reactjs.org/docs/hooks-reference.html#conditionally-firing-an-effect) and that `useEffect(cbk)` [runs every time](https://reactjs.org/docs/hooks-reference.html#useeffect).
Using this package avoids that confusion.## Installation
```bash
npm install --save use-once# Or
yarn add use-once
```## Usage
```js
import {useOnce} from 'use-once'function ReactComp(props) {
useOnce(() => {
console.log('This will only be run once for each instance of the component, instead of after every update.')return () => {
console.log('Returning a function is optional. The returned function will be called when the component is unmounted.')
}
})return null
}
```