@gravity-ui/graph
Version:
Modern graph editor component
23 lines (22 loc) • 559 B
JavaScript
import { useEffect, useRef } from "react";
/**
* Hook for tracking the previous value of a state.
*
* @example
* ```tsx
* const [count, setCount] = useState(0);
* const prevCount = usePrevious(count);
*
* ```
*
* @template T - Type of the value being tracked
* @param value - Current value whose previous state needs to be tracked
* @returns Previous value or undefined on first render
*/
export function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}