2022-05-30 21:10:03 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
|
2022-05-31 02:49:19 +08:00
|
|
|
/**
|
|
|
|
* Custom hook to get previous value. It can be used,
|
|
|
|
* for example, to get previous props or state.
|
2024-01-16 03:49:09 +08:00
|
|
|
* @param {*} value Value to be tracked
|
2022-05-31 02:49:19 +08:00
|
|
|
* @returns The previous value.
|
|
|
|
*/
|
2024-01-16 03:49:09 +08:00
|
|
|
export const usePreviousValue = <T = unknown>(value: T) => {
|
|
|
|
const ref = useRef<T>();
|
2022-05-30 21:10:03 +08:00
|
|
|
useEffect(() => {
|
|
|
|
ref.current = value;
|
|
|
|
});
|
|
|
|
return ref.current;
|
2023-07-20 00:22:50 +08:00
|
|
|
};
|
2022-05-30 21:10:03 +08:00
|
|
|
|
2024-03-09 04:48:44 +08:00
|
|
|
export default usePreviousValue;
|