Spaces:
Sleeping
Sleeping
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; | |
}; | |