Spaces:
Runtime error
Runtime error
File size: 664 Bytes
159e7fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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;
};
|