react-native-flip-book
Version:
19 lines (14 loc) • 595 B
text/typescript
import { useEffect, useRef } from 'react';
// Hook
const usePrevious = <T>(value: T) => {
// The ref object is a generic container whose current property is mutable ...
// ... and can hold any value, similar to an instance property on a class
const ref = useRef<T | undefined>(undefined); // Initialize with undefined
// Store current value in ref
useEffect(() => {
ref.current = value;
}, [value]); // Only re-run if value changes
// Return previous value (happens before update in useEffect above)
return ref.current;
};
export default usePrevious;