useDidUpdateEffect
🧵 do not worry about render phase effect calls
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);
};