use-did-update

Call function in useEffect when value changes, but not when component mounts
Import

Usage

When component mounts useEffect hook is called. This is fine in most cases, but if you need to track value changes you will need to implement something like this to prevent useEffect call on mount:

const mounted = useRef(false);
useEffect(() => {
if (mounted.current) {
fn();
} else {
mounted.current = true;
}
}, dependencies);

use-did-update uses the same logic:

useDidUpdate(() => console.log("Won't be called when mounted"), [value]);

Definition

function useDidUpdate(fn: () => void, dependencies?: any[]): void;