From 47274328c19147ab82bf6d78ae5bb9a9750004d8 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Wed, 3 Jan 2024 13:29:28 +0000 Subject: [PATCH] Add `warnIfNoStore` option --- .changeset/polite-crews-wave.md | 5 +++++ packages/jotai-x/src/createAtomStore.ts | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 .changeset/polite-crews-wave.md diff --git a/.changeset/polite-crews-wave.md b/.changeset/polite-crews-wave.md new file mode 100644 index 0000000..891cfc2 --- /dev/null +++ b/.changeset/polite-crews-wave.md @@ -0,0 +1,5 @@ +--- +'jotai-x': minor +--- + +Add `warnIfNoStore` option to `UseAtomOptions` diff --git a/packages/jotai-x/src/createAtomStore.ts b/packages/jotai-x/src/createAtomStore.ts index 8f5f769..4df6c02 100644 --- a/packages/jotai-x/src/createAtomStore.ts +++ b/packages/jotai-x/src/createAtomStore.ts @@ -14,6 +14,7 @@ export type UseAtomOptions = { scope?: string; store?: JotaiStore; delay?: number; + warnIfNoStore?: boolean; }; type UseAtomOptionsOrScope = UseAtomOptions | string; @@ -225,19 +226,23 @@ export const createAtomStore = < const setAtoms = {} as SetRecord; const useAtoms = {} as UseRecord; - const useStore = ( - optionsOrScope: UseAtomOptionsOrScope = {}, - warnIfUndefined = true - ) => { - const { scope, store } = convertScopeShorthand(optionsOrScope); - const contextStore = useAtomStore(name, scope, warnIfUndefined); + const useStore = (optionsOrScope: UseAtomOptionsOrScope = {}) => { + const { + scope, + store, + warnIfNoStore = true, + } = convertScopeShorthand(optionsOrScope); + const contextStore = useAtomStore(name, scope, !store && warnIfNoStore); return store ?? contextStore; }; const useAtomValueWithStore: GetAtomFn = (atomConfig, optionsOrScope) => { - const store = useStore(optionsOrScope, false); - const { delay = delayRoot } = convertScopeShorthand(optionsOrScope); - return useAtomValue(atomConfig, { store, delay }); + const options = convertScopeShorthand(optionsOrScope); + const store = useStore({ warnIfNoStore: false, ...options }); + return useAtomValue(atomConfig, { + store, + delay: options.delay ?? delayRoot, + }); }; const useSetAtomWithStore: SetAtomFn = (atomConfig, optionsOrScope) => {