How to handle deeply recursive state #2642
-
Just wondering the best way to handle not just nested but potentially recursively nested state in jotai? I have set up a code sandbox to help illustrate the question - code here . The important construct to model is the IComponent interface -
We have an editing environment that would allow you to add components to another component, and potentially edit the properties of any component anywhere in the component hierarchy via a property grid. So, in the interface above, the "properties" prop and the "content" prop are both able to be edited. I was wondering if the best way to handle this to minimize re-rendering would be to normalize this state to something like the following -
where the content prop is now just an array of references to other component ids. And then the jotai representation of a component would be something like -
And then finally, store the NormalizedComponentAtom's in either an atomFamily or even just a simple WeakMap (since I don't really need to track updates to the full component list anywhere - except maybe for undo/redo) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I'm not sure if there's single best way for it. |
Beta Was this translation helpful? Give feedback.
-
Assuming you read the docs, take a look at the ToDo example. export type PropsAtom = PrimitiveAtom<Record<string, unknown>>; That's not atoms in atom. It's like export type PropAtoms = Record<string, PrimitiveAtom<unknown>>; |
Beta Was this translation helpful? Give feedback.
I'm not sure if there's single best way for it.
I would prefer atoms in atom model without normalizing, but normalized approach would be a nice one too.