question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AtomFamily no way to separate key and init value?

See original GitHub issue

Maybe I’m missing something here, but it looks like atomFamily sets map key AND the atom value directly based from the param/ key and there’s no way to pass a key and value separately. Yes, its not 100% 1:1 since you can do get calls and derivative logic.

from atomFamily.ts:50-52

const newAtom = initializeAtom(param)
atoms.set(param, [newAtom, Date.now()])
return newAtom

So say I want to put an object in the atomFamily, I would need to have access to the entire object any time I want to access the atom. Doesn’t this make atomFamily nearly pointless?

How I would expect it to work is that I can pass in say a string key and that would give me access to the object.

Alternatively if there’s another way to create atoms dynamically that you can

  1. iterate over (doing it now inelegantly by tracking string IDs in another atom)
  2. access single atom by some ID/ key

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
dai-shicommented, Aug 16, 2021

Yes, no, yes.

const bigAtom = atom({
  objectID1: {
    key1: value1,
    key2: value2,
  },
  objectID2: {},
  objectID3: {}
  ...
})

const smallAtom = selectAtom(bitAtom, value => value.objectID1)
// will create something like the following.
const smallAtom = atom({
  key1: value1,
  key2: value2,
})
// but it's read-only. focusAtom will create a read-write atom.
0reactions
philipaarsethcommented, Aug 16, 2021

Would calling it like this useAtom(selectAtom(bigAtom let you solely subscribe to changes in the small atom and ignore all changes to bigAtom?

Also, wouldn’t it have to look like this?

const bigAtom = atom({
  objectID1: atom({
    key1: value1,
    key2: value2,
  }),
  objectID2: atom({}),
  objectID3: atom({}),
  ...
})

or does selectAtom automatically anything into a subscribable atom without the source having to be an atom beforehand?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question: atomFamily and key. #183 - GitHub
Is there any way to get a element by key? How does the key applies on a atomFamily? Does atomFamily supports a dynamic...
Read more >
atomFamily(options) - Recoil
key - A unique string used to identify the atom internally. This string should be unique with respect to other atoms and selectors...
Read more >
How to get all elements from an atomFamily in recoil?
You have to track all ids of the atomFamily to get all members of the family. Keep in mind that this is not...
Read more >
atomFamily — Jotai, primitive and flexible state management ...
Internally, atomFamily is just a Map whose key is a param and whose value is an atom config. Unless you explicitly remove unused...
Read more >
Jotai vs. Recoil: What are the differences? - LogRocket Blog
Atoms in Jotai do not have the key property, so instead of const ... In Recoil, the initial value is set in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found