Is there no way to iterate over a map of atoms and get the values? #2955
-
Hello, I am working on a project in React TS and using Jotai and Jotai-Form for state management. We have a large form that we are managing and in one section there dynamically rendered components that users can add or remove, in those components are form fields using atoms. I want to create a map atom that holds the form atoms (they are atomWithFormControl atoms from jotai-forms). I need to be able to iterate over the map and access the values to display the correct type of component per value. Add new values with custom parameters such as name and type along with the key for the map. And get the values based on the key. The problems I have been running into are, I can't figure out a way to iterate over a map of atoms and get the values as an array. Atom families were not helpful because I can't iterate over them and defining custom setters was messy bcs I needed to add extra props when initializing. Then I was defining a custom map class as an atom, but I couldn't iterate over those values either because of the hook issue. Is there no way to iterate over a map of atoms and get the values as an array of values? Keep in mind the atoms I am using are atomWithFormControls from the jotai-form library, and they will be updating frequently and separately after being added to the map. I would really appreciate any help on this |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
const fooFamily = atomFamily((param) => {
// family logic
})
const fooFamilyParams = fooFamily.getParams()
const fooFamilyAtoms = Array.from(fooFamilyParams).map(fooFamily)
const allAtomsSet = new Set()
const customStore = createStore().unstable_derive((...storeArgs) => {
const getAtomState = storeArgs[0]
storeArgs[0] = (a) => {
allAtomsSet.add(a)
return getAtomState(a)
}
return storeArgs
})
function JotaiCustomProvider() {
return <Provider store={allAtomsSet}>{children}</Provider>
} |
Beta Was this translation helpful? Give feedback.
unstable_derive
to store a list of "all atoms".