mirror of
https://github.com/zoo-labs/zoo.git
synced 2026-11-10 22:32:34 +00:00
17 lines
556 B
TypeScript
17 lines
556 B
TypeScript
import { useEffect, useRef } from 'react'
|
|
|
|
// modified from https://usehooks.com/usePrevious/
|
|
export default function 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 | null>(null)
|
|
|
|
// 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
|
|
}
|