0

I'm having trouble figuring out how to make a number of Recoil atoms state's interdependent.

For example, (this is not my use case, but it's a simple demonstration of the concept I'm having trouble with), imagine a radio button group where the state of each individual radio button is held by an atom<boolean>. And that the radio button's are all far away from each other on the react tree and don't share a parent.

When one atom's state changes to true, all other atom's state's need to set themselves to false. How could something like this be implemented when all of the atoms are distributed?

(For my actual case, my atom's contain objects rather than booleans, and the other atoms need to change in certain ways depending on both a field in that object, as well as which atom changed.)

SnippyDippy
  • 83
  • 1
  • 6

1 Answers1

0

I ended up solving this by creating a selectorFamily around my atomFamily, and then only interacting with the selectorFamily instead of the atomFamily directly.

The selectorFamily's set() property has ({get, set}) params that can be used to interact with multiple of the atoms generated by my atomFamily. In my set(), I use the family param, incoming state, and existing states (acquired with the provided get()) in order to conditionally perform the state transitions that I require.

SnippyDippy
  • 83
  • 1
  • 6