4 ms·
This is a general question, but how do you structure state with hooks if your React app is mostly composed of hierarchical, dynamic components? For example, le
by rajangdavis 6y ago
This is a general question, but how do you structure state with hooks if your React app is mostly composed of hierarchical, dynamic components?
For example, let's say the initial UI lets you create one or more of Component A (only one type of component); Component A can create one or more Component B (which can be many types of components); and Component B can create one or more of Component C (which can be many types of components).
The app is to model some configuration for some physical devices that are interconnected. I have something working today using the "useImmerReducer" package and updating parent components whenever children components change, but was curious if there is a more established pattern?
- aenero 6y agoDepending on which principles/patterns you are applying, you can go as elaborate as using something like Recoil for that (I think this might be a great application of Recoil as it deals with atomic data), or you can choose to go simple with a tree-type data structure that parallels your component tree. But first thing you should identify, IMHO, is which of those states are _really_ "shared" states. Coz some of those state values are probably better kept local.
- rajangdavis 6y agoI was looking at Recoil this weekend and it does seem like a good use case, I just wasn't sure how it would simplify what I was doing. I think I may have applied a bad design as I converted all of my React Components to React functions. Is it possible to instantiate Recoil atoms inside of for loops? Basically, I would like to be able to scope state locally to React functions, but hooks don't let you do that, they have to be completely abstracted.
- aenero 6y agoIt would be counter-intuitive if I endorse a Recoil-based solution on this thread that talks about a different library, LOL. But since I want to help as much as I can, my advice is, regardless of which library you choose, or which approach (atomic vs. monolithic/structured)... start with analyzing which parts of the state in your app really are supposed to be "shared" state. I have a feeling you may be trying to use "shared" state libraries to manage state that only the individual components would actually need. Do those dynamic components actually need to share data? The answer to that is the first important consideration, IMHO, before you can go forward. Good luck.