rooks
Version:
Essential React custom hooks ⚓ to super charge your components!
22 lines (21 loc) • 748 B
JavaScript
import { useRef, useEffect } from "react";
/**
* usePreviousDifferent hook for React
* It returns the past value which was different from the current one.
*
* @param currentValue The value whose previously different value is to be tracked
* @returns The previous value
* @see https://react-hooks.org/docs/usePreviousDifferent
*/
function usePreviousDifferent(currentValue) {
var previousRef = useRef(null);
var previousRef2 = useRef(null);
useEffect(function () {
previousRef2.current = previousRef.current;
previousRef.current = currentValue;
}, [currentValue]);
return currentValue === previousRef.current
? previousRef2.current
: previousRef.current;
}
export { usePreviousDifferent };