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.
|
|
|
|
* @param {*} value
|
|
|
|
* @returns The previous value.
|
|
|
|
*/
|
|
|
|
export const usePreviousValue = (value) => {
|
2022-05-30 21:10:03 +08:00
|
|
|
const ref = useRef();
|
|
|
|
useEffect(() => {
|
|
|
|
ref.current = value;
|
|
|
|
});
|
|
|
|
return ref.current;
|
2023-07-20 00:22:50 +08:00
|
|
|
};
|
2022-05-30 21:10:03 +08:00
|
|
|
|
|
|
|
export default {
|
2022-05-31 02:49:19 +08:00
|
|
|
usePreviousValue,
|
2022-05-30 21:10:03 +08:00
|
|
|
};
|