mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-14 16:28:59 +00:00
Port 9addad8ce5 to glitch-soc
Co-authored-by: diondiondion <mail@diondiondion.com>
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
27 lines
587 B
TypeScript
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;
|
|
}
|