pmndrs / jotai

👻 Primitive and flexible state management for React
https://jotai.org
MIT License
18.41k stars 587 forks source link

Atom scope on waitForAll and splitAtom is missing #391

Closed Thisen closed 3 years ago

garretteklof commented 3 years ago

Is this fixed? I'm getting scope: undefined when using splitAtom in 0.16.0

dai-shi commented 3 years ago

So, you specify an atom scope? Sounds like a new/unknown bug. Can you create a minimal repro?

garretteklof commented 3 years ago

Related to my comment here .. I'm trying to take an atom returned from atomWithQuery and 'split it' with splitAtom. The problem becomes that I can't update the atoms in the split (can read them) and I noticed that scope was undefined and thought that may have something to do with it. I invoked a similar pattern with array of atoms elsewhere, major caveat being atomWithQuery is async. However I did try creating the atom in render in a memo, and still no dice. If I'm doing something gravely wrong, please enlighten.

dai-shi commented 3 years ago

The atom created with atomWithQuery is not a primitive atom. If you split it, it would give you read-only atoms (or if it doesn't, it's a typing bug.) I still hope you could make a minimal example that we can play with.

garretteklof commented 3 years ago

I see. Okay, well if they're read-only then that's why I can't write to them 😅 I can make a minimal example but I think you just answered the question. My wording with "invoked a similar pattern elsewhere" was probably confusing. That wasn't using RQ. This isn't a minimal example for what I was doing but I took the react-query example in the docs and just logged the atoms. Looks like there's a special write function (which I didn't see previously) but when the atomWithQuery is split, each does seem to have their own write function so overall just a little confusing. Docs could help. I feel a little far away with my overall command of the lib otherwise I would be certainly glad to help with docs, etc. You're by far one of the best OS maintainers out there. Many thanks for all your hard work.

dai-shi commented 3 years ago

Yeah, it's docs issue. I think splitAtom is by far confusing one when it comes to writable atoms. (maybe focusAtom too. related: #231.)

If an atom has type WritableAtom<Value, Value>, then splitAtom returns a list of writable atoms. Otherwise, it returns a list of read-only atoms. PrimitiveAtom<Value> covers WritableAtom<Value, Value>.

An atom returned by atomWithQuery is an writable atom, but it's like read-only atom + action, like WritableAtom<Value, Action>. It's not symmetric, so we can't make a derived atom writable with splitAtom/focusAtom.

Thanks. I hope you can eventually understand it and contribute to docs.

dai-shi commented 3 years ago

@garretteklof btw, if what you want is to modify query result on client only, atomWithDefault might help.