Skip to content

Commit

Permalink
Add useResilient
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbonnet committed Nov 2, 2023
1 parent 96259f7 commit 98248bd
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/hooks/useResilient.ts
Original file line number Diff line number Diff line change
@@ -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<T>(value: T | undefined): T | undefined {
const ref = useRef(value);
if (value !== undefined) {
ref.current = value;
}
return ref.current;
}

0 comments on commit 98248bd

Please sign in to comment.