diff --git a/lib/hooks/useResilient.ts b/lib/hooks/useResilient.ts new file mode 100644 index 00000000..a6d6a503 --- /dev/null +++ b/lib/hooks/useResilient.ts @@ -0,0 +1,15 @@ +import { useRef } from "../dependencies"; + +/** + * Returns the last non-undefined version of the provided `value`, or `undefined`. + * + * @param value The `value` to keep a non-undefined value of. + * @returns The last non-undefined version of the provided `value`, or `undefined`. + */ +export function useResilient(value: T | undefined): T | undefined { + const ref = useRef(value); + if (value !== undefined) { + ref.current = value; + } + return ref.current; +}