vision-agent / lib /hooks /usePrevious.ts
MingruiZhang's picture
feat: Add shark example for quicker test / add time counter / add carousel (#77)
abc1963 unverified
raw
history blame contribute delete
No virus
664 Bytes
import { useEffect, useRef } from 'react';
/**
* A simple hook to return the previous state of a useState
* https://usehooks.com/usePrevious/
* @param value the current state
* @returns the previous state
*/
export 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>();
// 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;
};