Skip to content

Commit

Permalink
feat: export useStore hook using the isolated context (#27)
Browse files Browse the repository at this point in the history
* fix: export store to allow other hooks to use the context

* style: space

* fix: PR comments

* fix: remove default store

* fix: remove import

* feat: use original hook

Co-authored-by: Daishi Kato <[email protected]>

---------

Co-authored-by: Daishi Kato <[email protected]>
  • Loading branch information
marcocondrache and dai-shi authored Apr 29, 2024
1 parent 90e276c commit 80884b4
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/createIsolation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useAtom as useAtomOrig,
useAtomValue as useAtomValueOrig,
useSetAtom as useSetAtomOrig,
useStore as useStoreOrig,
} from 'jotai/react';
import { useHydrateAtoms } from 'jotai/react/utils';

Expand Down Expand Up @@ -36,29 +37,26 @@ export function createIsolation() {
);
};

const useAtom = ((anAtom: any, options?: any) => {
const useStore = ((options?: any) => {
const store = useContext(StoreContext);
if (!store) {
throw new Error('Missing Provider from createIsolation');
}
if (!store) throw new Error('Missing Provider from createIsolation');
return useStoreOrig({ store, ...options });
}) as typeof useStoreOrig;

const useAtom = ((anAtom: any, options?: any) => {
const store = useStore();
return useAtomOrig(anAtom, { store, ...options });
}) as typeof useAtomOrig;

const useAtomValue = ((anAtom: any, options?: any) => {
const store = useContext(StoreContext);
if (!store) {
throw new Error('Missing Provider from createIsolation');
}
const store = useStore();
return useAtomValueOrig(anAtom, { store, ...options });
}) as typeof useAtomValueOrig;

const useSetAtom = ((anAtom: any, options?: any) => {
const store = useContext(StoreContext);
if (!store) {
throw new Error('Missing Provider from createIsolation');
}
const store = useStore();
return useSetAtomOrig(anAtom, { store, ...options });
}) as typeof useSetAtomOrig;

return { Provider, useAtom, useAtomValue, useSetAtom };
return { Provider, useStore, useAtom, useAtomValue, useSetAtom };
}

0 comments on commit 80884b4

Please sign in to comment.