From 80884b4905fbd1544b0afa5a8a82a971d898fb45 Mon Sep 17 00:00:00 2001 From: Marco Mihai Condrache <52580954+marcocondrache@users.noreply.github.com> Date: Mon, 29 Apr 2024 04:27:43 +0200 Subject: [PATCH] feat: export useStore hook using the isolated context (#27) * 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 --------- Co-authored-by: Daishi Kato --- src/createIsolation.tsx | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/createIsolation.tsx b/src/createIsolation.tsx index a4c677c..784905f 100644 --- a/src/createIsolation.tsx +++ b/src/createIsolation.tsx @@ -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'; @@ -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 }; }