@carbon/react
Version:
React components for the Carbon Design System
29 lines (25 loc) • 654 B
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { useRef, useEffect } from 'react';
/**
* Stores the previous value of a given input.
*
* @param value - The current value.
* @returns The value before the current render.
*
* @example
* const [count, setCount] = useState(0);
* const prevCount = usePreviousValue(count);
*/
const usePreviousValue = value => {
const ref = useRef(undefined);
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
};
export { usePreviousValue };