Skip to content

Commit

Permalink
Merge pull request #4 from udecode/feat/warn-if-no-store
Browse files Browse the repository at this point in the history
Add `warnIfNoStore` option
  • Loading branch information
12joan authored Jan 3, 2024
2 parents fd7714b + 4727432 commit 4e17e1d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-crews-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jotai-x': minor
---

Add `warnIfNoStore` option to `UseAtomOptions`
23 changes: 14 additions & 9 deletions packages/jotai-x/src/createAtomStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type UseAtomOptions = {
scope?: string;
store?: JotaiStore;
delay?: number;
warnIfNoStore?: boolean;
};

type UseAtomOptionsOrScope = UseAtomOptions | string;
Expand Down Expand Up @@ -225,19 +226,23 @@ export const createAtomStore = <
const setAtoms = {} as SetRecord<MyWritableStoreAtoms>;
const useAtoms = {} as UseRecord<MyWritableStoreAtoms>;

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

0 comments on commit 4e17e1d

Please sign in to comment.