Files
mastodon/app/javascript/flavours/glitch/hooks/usePrevious.ts
Renaud Chaput 5b75667c03 [Glitch] Update to latest eslint-plugin-react-hooks
Port 9addad8ce5 to glitch-soc

Co-authored-by: diondiondion <mail@diondiondion.com>
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
2025-11-10 22:29:58 +01: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;
}