TelegramPWA/src/hooks/usePrevious.ts
Alexander Zinchuk 3afcde3217 Initial commit
2021-04-09 14:11:51 +03:00

17 lines
589 B
TypeScript

import { useRef } from '../lib/teact/teact';
function usePrevious<T extends any>(next: T): T | undefined;
function usePrevious<T extends any>(next: T, shouldSkipUndefined: true): Exclude<T, undefined> | undefined;
function usePrevious<T extends any>(next: T, shouldSkipUndefined?: boolean): Exclude<T, undefined> | undefined;
function usePrevious<T extends any>(next: T, shouldSkipUndefined?: boolean) {
const ref = useRef<T>();
const { current } = ref;
if (!shouldSkipUndefined || next !== undefined) {
ref.current = next;
}
return current;
}
export default usePrevious;