Skip to content

Commit

Permalink
Throw error on useAtom(undefined) or useAtom(null) (pmndrs#2778)
Browse files Browse the repository at this point in the history
* Fix undefined or null atoms

* Simplify conditional, only in dev mode

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

* Update useSetAtom.ts

* move warning to store.ts

* move the warning in getAtomState

---------

Co-authored-by: Daishi Kato <[email protected]>
Co-authored-by: daishi <[email protected]>
  • Loading branch information
3 people authored and dmaskasky committed Nov 14, 2024
1 parent 33260c6 commit db988fe
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,9 @@ const buildStore = (
export const createStore = (): Store => {
const atomStateMap = new WeakMap()
const getAtomState = <Value>(atom: Atom<Value>) => {
if (import.meta.env?.MODE !== 'production' && !atom) {
throw new Error('Atom is undefined or null')
}
let atomState = atomStateMap.get(atom) as AtomState<Value> | undefined
if (!atomState) {
atomState = { d: new Map(), p: new Set(), n: 0 }
Expand Down

0 comments on commit db988fe

Please sign in to comment.