"use client"; import * as React from "react"; /** * Tracks and returns the previous value of a state or prop. * * @remarks * This hook stores the value from the previous render cycle, allowing you to compare * current and previous values. On the initial render, it returns `undefined` since * there is no previous value yet. * * Useful for detecting changes, implementing undo functionality, or creating * animations based on value transitions. * * @typeParam T - The type of the value being tracked. * @param value - The current value to track. * @returns The value from the previous render, or `undefined` on the first render. * * @example * ```tsx * function Counter() { * const [count, setCount] = useState(0); * const previousCount = usePrevious(count); * * return ( *
*

Current: {count}

*

Previous: {previousCount ?? "N/A"}

* *
* ); * } * ``` */ export function usePrevious(value: T): T | undefined { const ref = React.useRef(undefined); React.useEffect(() => { ref.current = value; }, [value]); return ref.current; }