diff --git a/src/Utils.tsx b/src/Utils.tsx index 53412de..6147e56 100644 --- a/src/Utils.tsx +++ b/src/Utils.tsx @@ -1,3 +1,4 @@ +import {useEffect, useRef} from "react"; export type Modify = Omit & R; @@ -31,3 +32,21 @@ export function normalizeString(str: string): string .replace?.(/[\u0300-\u036f]/g, "") : ""; } + +/** + * Get the previous value of a given value. + * @param currentValue The current value. + */ +export function usePreviousValue(currentValue: T): T +{ + // Get the reference to the previous value. + const ref = useRef(); + + // If the value has changed, saving it in the reference after rendering. + useEffect(() => { + ref.current = currentValue; + }, [currentValue]); + + // Get the previous value. + return ref?.current ?? undefined; +}