reactjsreact-hooksjotai

How to read data from an array of atoms in Jotai state management library


I have an array of atoms, I need to get the data from each of those atoms and store it in a new array.

As you can see in the image I am getting an array of atoms from requestAtoms and then I have to create a new array from the atoms that I get from request atoms that should contain data (not the atoms) and then I want to post it to the backend.

I want to handle this in an handle submit function and since I can not use hooks inside JavaScript function I can not use useAtom in handleSubmit and even if I use useAtom outside how can I iterate over each atom and apply useAtom on it.

Please help!!!


Solution

  • This is not possible with Jotai and especially with Hooks, you can't put useAtom in the loop and update individual elements when iterating over your list. What you could do, is to create a "derived" atom, that is backend by a list of another atoms and then update this derived atom, so that it updates its underlying collection of atoms.