beautiful-react-hooks
Version:
A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development
16 lines (15 loc) • 406 B
JavaScript
import { useEffect, useRef } from 'react';
/**
* On each render returns the previous value of the given variable/constant.
*/
const usePreviousValue = (value) => {
const prevValue = useRef();
useEffect(() => {
prevValue.current = value;
return () => {
prevValue.current = undefined;
};
});
return prevValue.current;
};
export default usePreviousValue;