@carbon/react
Version:
React components for the Carbon Design System
35 lines (33 loc) • 859 B
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* 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 { useEffect, useRef } from "react";
//#region src/internal/usePreviousValue.ts
/**
* Copyright IBM Corp. 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* 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(void 0);
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
};
//#endregion
export { usePreviousValue };