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.

Best way to use atomFamily/selectorFamily

See original GitHub issue

Hi there! Really love working with recoil, it’s been fun implementing it in a lil documents app I’m building.

I am trying to get my head around the best ways to model data with recoil. Currently I have atoms set up such as:

const items = atom<EntityState<SingleItem>>({
  key: 'items',
  default: {
    ids: [],
    entities: {},
  },
});

And when I’m adding a new item, it generates an ID and pushes them to the array, then the single item lives in entities[id] = item.

I feel like things would be better if I used atomFamily, but I am not clear on how to get a list of all the ids within a type of atom/family.

Additionally, if I have the base atom of the item, which contains the entries and ids, does it make sense to then use atomFamily/selectorFamily to use that as the source of data? In that situation, would anything that did something like const singleItem = useRecoilValue(itemsFamily(id) subscribe to just the specific item?

Thanks for any guidance!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
Shmewcommented, Jun 10, 2020

What I like to do in this situation where you need to keep track of all ids is to have an atom of ids and entities, and an atomFamily that given an id returns the entity from that atom. This lets you subscribe to specific ids, and if you need more than one you can create a selectorFamily that given a collection of ids it aggregates the atoms into a singular value.

0reactions
drarmstrcommented, Jun 16, 2020

Families just represent a collection of atoms or selectors based on some parameter. They are useful for passing parameters into selector functions or for creating more granular atoms, since atoms and selectors are the unit of resolution for controlling component updates.

Read more comments on GitHub >

github_iconTop Results From Across the Web

atomFamily(options) - Recoil
An Atom Family represents a collection of atoms. ... For dynamic defaults based on other state use a selectorFamily() , which also has...
Read more >
How to get all elements from an atomFamily in recoil?
You can use an atom to track the ids of each atom in the atomFamily . Then use a selectorFamily or a custom...
Read more >
Use selectorFamily to take arguments in your Recoil selectors
In this lesson, we're going to learn how to create Recoil selectors that accept arguments. These are made possible using the selectorFamily ......
Read more >
Atom Family in Recoil for Statement Management — Nextjs
This blog post will teach you how to use the recoil atom family to ... and then you used a selector family for...
Read more >
Daishi Kato on Twitter: "Just wrote full responses to a question ...
I use atomFamily/selectorFamily a lot within Recoil. Is there any recommended way to use atom with param to replace Recoil's atomFamily ...
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