@snappmarket/use-did-update-effect

> 🧵 do not worry about render phase effect calls ----

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@snappmarket/use-did-update-effect
37170.2.54 years ago4 years agoMinified + gzip package size for @snappmarket/use-did-update-effect in KB

Readme

useDidUpdateEffect
🧵 do not worry about render phase effect calls
----
version downloads PRs Welcome MIT License
Watch on GitHub Star on GitHub

get started

We provide two way of using this package single or multi :
npm i @snappmarket/use-did-update-effect
OR
npm i @snappmarket/hooks

usage

import useDidUpdateEffect from '@snappmarket/use-did-update-effect';
// or 
// import { useDidUpdateEffect } from '@snappmarket/hooks';


const MyComponenet = props => {
  /**
   * Do not run on render
   */
  useDidUpdateEffect(() => {
    // I will run only when my dependencies update
  }, [deps]);
};

source code

import { useRef, useEffect } from 'react';

/**
 * Calls function on component update or inputs change phase
 * @param fn
 * @param inputs
 */
export default (fn, inputs) => {
  const didMountRef = useRef(false);

  useEffect(() => {
    if (didMountRef.current) fn();
    else didMountRef.current = true;
  }, inputs);
};