Skip to content

Commit

Permalink
fix(store): improve dual instance warning in DEV (#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Mar 29, 2024
1 parent b74d6d1 commit dbed0aa
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,25 +828,17 @@ type Store = ReturnType<typeof createStore>

let defaultStore: Store | undefined

if (import.meta.env?.MODE !== 'production') {
if (typeof (globalThis as any).__NUMBER_OF_JOTAI_INSTANCES__ === 'number') {
++(globalThis as any).__NUMBER_OF_JOTAI_INSTANCES__
} else {
;(globalThis as any).__NUMBER_OF_JOTAI_INSTANCES__ = 1
}
}

export const getDefaultStore = () => {
if (!defaultStore) {
if (
import.meta.env?.MODE !== 'production' &&
(globalThis as any).__NUMBER_OF_JOTAI_INSTANCES__ !== 1
) {
console.warn(
'Detected multiple Jotai instances. It may cause unexpected behavior with the default store. https://github.com/pmndrs/jotai/discussions/2044',
)
}
defaultStore = createStore()
if (import.meta.env?.MODE !== 'production') {
;(globalThis as any).__JOTAI_DEFAULT_STORE__ ||= defaultStore
if ((globalThis as any).__JOTAI_DEFAULT_STORE__ !== defaultStore) {
console.warn(
'Detected multiple Jotai instances. It may cause unexpected behavior with the default store. https://github.com/pmndrs/jotai/discussions/2044',
)
}
}
}
return defaultStore
}

0 comments on commit dbed0aa

Please sign in to comment.