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) => {