mango-v4-ui/components/shared/usePrevious.tsx

17 lines
558 B
TypeScript
Raw Normal View History

2022-09-13 23:24:26 -07:00
import { useEffect, useRef } from 'react'
2023-02-27 23:20:11 -08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-09-13 23:24:26 -07:00
export default function usePrevious(value: any): any {
// 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()
// 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
}