Is there any way to get the contents of another atom with jotai optics in a focus atom? #2719
Unanswered
Aubrey-Russell
asked this question in
General
Replies: 2 comments 5 replies
-
You can't, but you can create an atom in atom. const atomInAtom = atom((get) => focusAtom(...)) However, it's tricky to use. I wonder if you have a solution with focusAtom with combined atom. const combinedAtom = atom(
(get) => ({ someState: get(someStateAtom), currentSelectedId: get(currentSelectedIdAtom) }),
// we need `write` too
)
const focusedState = focusAtom(combinedAtom, (optic) => ...) Otherwise, I'd give up using focusAtom. |
Beta Was this translation helpful? Give feedback.
1 reply
-
@dai-shi This is what I came up with based on your suggestion but I had to create a new atom utility called derived atom to get this to work cleanly in the component itself:
and my useDerivedAtom utility:
and then I can use the atom like any other:
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
My current approach is to have a derived atom which gets the selected index I want, then grabs the correct atom from my split atom array. Then I have a focus atom that uses that derived atom.
However, I was wondering if there was any way to get the contents of another atom as a step in a focus atom? For example this fake pseudo code:
The fake element is this part:
.jotaiFilter((item, get) => item.id === get(currentSelectedId))
where there is a step to filter the elements based on the contents of another atom. is such a thing possible?
Beta Was this translation helpful? Give feedback.
All reactions