UNPKG

719 BJavaScriptView Raw
1import * as React from 'react';
2
3/**
4 * @ignore - internal hook.
5 *
6 * Initializes a ref with the given value and updates it when the value changes.
7 *
8 * @param value Value to store in the ref
9 * @param deps An optional array of dependencies to watch for changes. If not provided, the ref will be updated each time the `value` changes.
10 * @returns A React.RefObject containing the latest value
11 *
12 * API:
13 *
14 * - [useLatest API](https://mui.com/base/api/use-latest/)
15 */
16export default function useLatest(value, deps) {
17 const ref = React.useRef(value);
18 React.useEffect(() => {
19 ref.current = value;
20 // eslint-disable-next-line react-hooks/exhaustive-deps
21 }, deps != null ? deps : [value]);
22 return ref;
23}
\No newline at end of file