Renaud Chaput 9addad8ce5
Update to latest eslint-plugin-react-hooks (#36702)
Co-authored-by: diondiondion <mail@diondiondion.com>
2025-11-10 14:50:04 +00:00

27 lines
587 B
TypeScript

import { useState } from 'react';
/**
* Returns the previous state of the passed in value.
* On first render, undefined is returned.
*/
export function usePrevious<T>(value: T): T | undefined {
const [{ previous, current }, setMemory] = useState<{
previous: T | undefined;
current: T;
}>(() => ({ previous: undefined, current: value }));
let result = previous;
if (value !== current) {
setMemory({
previous: current,
current: value,
});
// Ensure that the returned result updates synchronously
result = current;
}
return result;
}