I did my first experiment with recoil, building an editable table. Each cell has an atom which stores its row, column, and text value. The way I built this was by
- initializing each cell's atom into a dictionary (just a plain object), with keys in a format of [column]x[row]
- I then iterate over these keys in the Table component, and pass only the key to each Cell component
- The Cell component uses
useRecoilState
and find its specific Atom by accessing the main dictionary using the key it got passed as a prop.
Now, it seems to me that this use case (creating thousands of related atoms with the same shape) is what atomFamily
is meant to make easier, but I don't understand how to use it in this way, where you initialize each atom with a specific value.
And, besides that, I don't understand what is the advantage of using atomFamily over storing a collection of atoms. I understand there is memoization involved, but I don't understand what is getting memoized other than, if I am reading correctly, the ability to recall a specific atom by calling the function again with the same id, which would get you pretty much the same behavior I'm getting with a dictionary.