/** * Tracks the previous value of a state or prop. * Returns undefined on the first render, then returns the previous value on subsequent renders. * * @template T - The type of the value being tracked * @param value - The current value to track * @returns The previous value, or undefined on first render * * @example * ```tsx * function Counter() { * const [count, setCount] = useState(0); * const prevCount = usePrevious(count); * * return ( *
*

Current count: {count}

*

Previous count: {prevCount ?? 'N/A'}

* *
* ); * } * ``` * * @example * ```tsx * function SearchResults({ query }: { query: string }) { * const prevQuery = usePrevious(query); * const [results, setResults] = useState([]); * * useEffect(() => { * if (query !== prevQuery) { * console.log(`Query changed from "${prevQuery}" to "${query}"`); * fetchResults(query).then(setResults); * } * }, [query, prevQuery]); * * return ; * } * ``` * * @example * ```tsx * function Animation({ isVisible }: { isVisible: boolean }) { * const wasVisible = usePrevious(isVisible); * * const animationClass = useMemo(() => { * if (isVisible && !wasVisible) return 'fade-in'; * if (!isVisible && wasVisible) return 'fade-out'; * return ''; * }, [isVisible, wasVisible]); * * return
Content
; * } * ``` */ export declare function usePrevious(value: T): T | undefined; /** * Tracks the previous value with a custom comparison function. * Only updates the previous value when the comparison function returns false. * * @template T - The type of the value being tracked * @param value - The current value to track * @param compare - Function that returns true if values should be considered equal * @returns The previous distinct value * * @example * ```tsx * function UserProfile({ user }: { user: User }) { * // Only update previous user when the ID changes * const prevUser = usePreviousDistinct( * user, * (prev, next) => prev?.id === next?.id * ); * * useEffect(() => { * if (prevUser && prevUser.id !== user.id) { * console.log(`User changed from ${prevUser.name} to ${user.name}`); * } * }, [user, prevUser]); * * return
{user.name}
; * } * ``` * * @example * ```tsx * function ArrayComponent({ items }: { items: string[] }) { * // Only track when array length changes * const prevItems = usePreviousDistinct( * items, * (prev, next) => prev?.length === next?.length * ); * * if (prevItems && prevItems.length !== items.length) { * console.log(`Array length changed from ${prevItems.length} to ${items.length}`); * } * * return ; * } * ``` */ export declare function usePreviousDistinct(value: T, compare: (prev: T | undefined, next: T) => boolean): T | undefined; /** * Tracks multiple previous values in a history array. * Useful for implementing undo/redo or tracking value changes over time. * * @template T - The type of the value being tracked * @param value - The current value to track * @param maxHistory - Maximum number of previous values to keep (default: 10) * @returns Array of previous values, most recent first * * @example * ```tsx * function DrawingCanvas() { * const [drawing, setDrawing] = useState([]); * const history = usePreviousHistory(drawing, 20); * * const undo = () => { * if (history.length > 0) { * setDrawing(history[0]); * } * }; * * return ( *
* * *
* ); * } * ``` * * @example * ```tsx * function TextEditor() { * const [text, setText] = useState(''); * const history = usePreviousHistory(text, 50); * * const showHistory = () => { * console.log('Recent changes:', history); * }; * * return ( *
*