-
Notifications
You must be signed in to change notification settings - Fork 1.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Question: how to deal with data structures after decomposition into atomFamily? #905
Comments
I think you have the right idea using useRecoilCallback and the snapshot, here is a sandbox that shows it working, not sure if it scales though. |
Thank you very much, very helpful! |
If you're looking at synchronizing atoms with a backing store you may also want to consider the atom effects interface. |
Another way of achieving this could be possible by using a const shapeIdsState = atom({ key: 'shapeIds', default: [] });
const shapeState = atomFamily({ key: 'shape', default: null });
const shapeFilterState = atom({ key: 'shapeFilter', default: null })
const colorFilterState = atom({ key: 'colorFilter', default: null })
const filteredIdsSelector = selector({
key: "filteredIdsSelector",
get({ get }) {
const ids = get(shapeIdsState);
const shapeFilter = get(shapeFilterState);
const colorFilter = get(colorFilterState);
if (!shapeFilter && !colorFilter) {
return ids;
}
return ids.filter((id) => {
const shape = get(shapeState(id))
// filtering logic goes here... (omitted for brevity)
});
}
});
function Shapes() {
const filteredIds = useRecoilValue(filteredIdsSelector);
return (
<>
{filteredIds.map(id => (
<Shape id={id} key={id} />
))}
</>
)
} |
There's a known issue with loops in selectors being slow and halting the program. I believe that selector would trigger that case. |
For ad-hoc usage to gather data from multiple atoms/selectors, there is also the |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Looking for guidance on the "correct" way to gather & iterate over data that's been decomposed into AtomFamily units.
Let's say I have an app that lets me draw shapes, and each shape's data is stored in an Atom, each atom being a part of an AtomFamily. I also have another Atom that has a list of the shape IDs so that I can iterate over the when doing the drawing. So far so good.
Now let's say I want to only display the blue rectangles. The color and shape is stored in the AtomFamily atoms, so the data is not immediately available.
The way my app works right now is that I have a data backing store (a JavaScript Map object) outside of the whole Recoil system. Every time I save a recoil atom I also push changes back into the backing store. Then, when I need to regenerate my "display list" I use that backing store.
My approach doesn't feel optimal, so I wanted to ask for your opinion of what a better approach should be. One possibility I came up with is to create a new list of shape IDs by "looking up" each atom, maybe inside a useRecoilCallback via the Snapshot property?
Is there a "correct" way of handling composite/complex data structures with recoil?
Thanks in advance for any guidance you can provide,
Greg
The text was updated successfully, but these errors were encountered: