From 98248bd45b2d856cf4cf3fd4e61b7527d90a5515 Mon Sep 17 00:00:00 2001 From: David Bonnet Date: Thu, 2 Nov 2023 09:37:12 +0100 Subject: [PATCH] Add `useResilient` --- lib/hooks/useResilient.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/hooks/useResilient.ts 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; +}