Files
Tom Moor 084490ba6b chore: Remove React in scope requirement (#9261)
* Add rules

* codemod: update-react-imports

* Update babelrc
2025-05-20 19:26:11 -04:00

22 lines
487 B
TypeScript

import { useRef, useEffect } from "react";
/**
* A hook to get the previous value of a variable.
*
* @param value The value to track.
* @param onlyTruthy Whether to include only truthy values.
* @returns The previous value of the variable.
*/
export default function usePrevious<T>(value: T, onlyTruthy = false): T | void {
const ref = useRef<T>();
useEffect(() => {
if (onlyTruthy && !value) {
return;
}
ref.current = value;
});
return ref.current;
}