use-once

1.0.1 • Public • Published

use-once

A helper for running react effects only once.

Motivation

For beginners, it isn't clear that useEffect(cbk, []) runs only once and that useEffect(cbk) runs every time. Using this package avoids that confusion.

Installation

npm install --save use-once
 
# Or 
 
yarn add use-once

Usage

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
}

Package Sidebar

Install

npm i use-once

Weekly Downloads

39

Version

1.0.1

License

MIT

Unpacked Size

3.3 kB

Total Files

10

Last publish

Collaborators

  • joeldenning