How to iterate over an atom family ? #1912
-
In my projects, I try to use atomFamily but I can't find how to iterate over its atoms. The sample in the doc use both the family and a second atom that contains an array of items ID. Is there a way to avoid this extra code ? Maybe by making atomFamily iterable by exposing its internal map ? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
atomFamily isn't iterable by design. // conceptually
const myAtoms = [atom(...), atom(...), ...]; Or, That said, my preference is atoms-in-atom pattern, as it allows to make the array reactive. |
Beta Was this translation helpful? Give feedback.
-
By atoms-in-atom you mean something like this : https://jotai.org/docs/guides/atoms-in-atom#storing-an-array-of-atom-configs-in-atom ? If I'm right, there's no |
Beta Was this translation helpful? Give feedback.
-
In case it helps, another way to do this is by creating an atom with a set of ids
``const myThingAtomFamily = atomFamily ...` Update those in the same time you're updating the contents:
And then use the set of Ids to iterate over the family. I guess I could just make an atom as a map of atoms, but I'm struggling a bit to make it work like that 😅 |
Beta Was this translation helpful? Give feedback.
atomFamily isn't iterable by design.
In case you want to iterate them, I think you can simply have an array on your end.
Or,
Map
would be fine too.That said, my preference is atoms-in-atom pattern, as it allows to make the array reactive.