Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

How to atomise nested array type data structure? #1585

Answered by honzabrecka
jeffski asked this question in Q&A
Discussion options

You must be logged in to vote

I'd suggest to have an atom to hold the root and one atomFamily for groups and one atomFamily for items:

const root = atom({
  key: "Root",
  default: { groups: [] } 
});
const groups = atomFamily({
  key: 'Group',
  default: { items: [] },
});
const items = atomFamily({
  key: 'Item',
  default: { name: "" },
});

// so your example normalized would be:
// note that ids are just name of entity + index
root: { groups: ["group1", "group2"] } 
groups: { "group1": { items: ["item1", "item2"] }, "group2": { items: ["item3", "item4"] } }
items: { "item1": { name: "item 1" }, "item2": { name: "item 2" }, ... }

and selectorFamily to denormalize data back to its original shape.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@jeffski
Comment options

@douglasg14b
Comment options

Answer selected by jeffski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants