From 6afbc0902836ef8f8af0fcf5494c47417de441fe Mon Sep 17 00:00:00 2001 From: Kevin Schaich Date: Mon, 11 Nov 2024 03:04:38 -0800 Subject: [PATCH] Throw error on `useAtom(undefined)` or `useAtom(null)` (#2778) * Fix undefined or null atoms * Simplify conditional, only in dev mode Co-authored-by: Daishi Kato * Update useSetAtom.ts * move warning to store.ts * move the warning in getAtomState --------- Co-authored-by: Daishi Kato Co-authored-by: daishi --- src/vanilla/store.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index 39e55795c6..5b0c47cf17 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -680,6 +680,9 @@ const buildStore = ( export const createStore = (): Store => { const atomStateMap = new WeakMap() const getAtomState = (atom: Atom) => { + if (import.meta.env?.MODE !== 'production' && !atom) { + throw new Error('Atom is undefined or null') + } let atomState = atomStateMap.get(atom) as AtomState | undefined if (!atomState) { atomState = { d: new Map(), p: new Set(), n: 0 }