useAtomValueSafe

This commit is contained in:
sawka 2024-07-08 18:30:11 -07:00
parent f4702cca29
commit c57c7ea220

View File

@ -3,6 +3,7 @@
import base64 from "base64-js"; import base64 from "base64-js";
import clsx from "clsx"; import clsx from "clsx";
import * as jotai from "jotai";
function isBlank(str: string): boolean { function isBlank(str: string): boolean {
return str == null || str == ""; return str == null || str == "";
@ -152,6 +153,15 @@ function jotaiLoadableValue<T>(value: Loadable<T>, def: T): T {
return def; return def;
} }
const NullAtom = jotai.atom(null);
function useAtomValueSafe<T>(atom: jotai.Atom<T>): T {
if (atom == null) {
return jotai.useAtomValue(NullAtom) as T;
}
return jotai.useAtomValue(atom);
}
export { export {
base64ToArray, base64ToArray,
base64ToString, base64ToString,
@ -163,4 +173,5 @@ export {
jsonDeepEqual, jsonDeepEqual,
makeIconClass, makeIconClass,
stringToBase64, stringToBase64,
useAtomValueSafe,
}; };