From 27f918f53c0c841ed1f9587e3f4340df75696213 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Wed, 2 Aug 2023 19:41:22 +0200 Subject: [PATCH] Make usePrism hot-reloadable --- packages/react/src/index.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts index 826448c..4859ca0 100644 --- a/packages/react/src/index.ts +++ b/packages/react/src/index.ts @@ -65,16 +65,16 @@ export function usePrism( atomRef.current.set(fnAsCallback) } - const prsm = useMemo( - () => - prism(() => { - const fn = atomRef.current.prism.getValue() - return fn() - }), - [], - ) + const prismRef = useRef | null>(null) - return usePrismInstance(prsm, debugLabel) + if (!prismRef.current) { + prismRef.current = prism(() => { + const fn = atomRef.current.prism.getValue() + return fn() + }) + } + + return usePrismInstance(prismRef.current, debugLabel) } export const useVal: typeof val = (p: $IntentionalAny, debugLabel?: string) => { @@ -346,6 +346,7 @@ export function usePrismInstance(der: Prism, debugLabel?: string): T { if (process.env.NODE_ENV !== 'production') { if (der !== ref.current.der) { + debugger console.error( 'Argument `der` in `usePrismInstance(der)` should not change between renders.', )