11bit / jotai-minidb

Jotai atoms factory for IndexedDb key-value storage
MIT License
19 stars 1 forks source link

Looking to use jotai-minidb with nextjs #5

Open chirag-mehta opened 8 months ago

chirag-mehta commented 8 months ago

Hey! I'm trying to use jotai-minidb with nextjs and keep running into "IndexedDb is not defined" error.

I used typeof window !== "undefined" to conditionally load MiniDb and its .item but I'm not sure what it's default value should be since if its undefined then I can't use it in useAtom.

If I store an atomWithStorage then the setter that I get from useAtom ends up having a "never" type.

11bit commented 8 months ago

Hey!

One option could be to initialize MiniDb only on a client like this:

const db = (typeof window !== 'undefined' ? new MiniDb() : undefined)!

Not the ! at the end. It will be undefined on server but the exclamation mark will make the types work. Of course, it has to never be used in SSR components

Hope it helps!