3 ms·
I have no problem with storing things like modal state, currently selected items, etc. in Redux, but I’m having trouble imagining a scenario where you would nee
by baddox 9y ago
I have no problem with storing things like modal state, currently selected items, etc. in Redux, but I’m having trouble imagining a scenario where you would need to generate a random ID to associate a piece of state with an arbitrary component. Generally the Redux state tree would store the piece of UI state inside the “model” it is associated with.
As a super simple example, if you had a list of continents, and each continent could be expanded (to list every country inside of it) or collapsed, your Redux state might look like:
{
'continents': [
'Asia': {
'collapsed': false,
'countries': [
'Mongolia',
'Japan',
// ...
],
},
'North American': {
'collapsed': true,
'countries': [
'Canada',
'Mexico',
// ...
],
},
],
}
The 'collapsed' key is purely UI state for your collapsible component (i.e. you probably wouldn't sync it to a server), but it is keyed naturally in the Redux state along with the normal Redux data it's related to.
In what case would you need to generate a random ID per component simply to "remember" where the data for each component is located in Redux?
- xatxat 9y agoFor example if you have multiple lists of continents and each one needs its own collapsed state. Maybe the user can even dynamically add/remove such continent lists in the UI. IMHO in most of these cases it's best to keep the continents data inside of redux and have the collapsed state being managed by the "ContinentsList" component.
- baddox 9y agoStill, wouldn’t the list of continent-lists be stored as an array in Redux? Yes, generally something like expanding an accordion component can just sit in the local component state, as long as the collapsed state doesn’t need to affect or be affected by other app state.
- xatxat 9y agoI wouldn't store it as array in Redux because that would be too limiting (e.g. let's say I want a continent-list which is decoupled from the other continent-lists). If I would be forced to use redux in this case then I would create some random id for each ComponentList where I would store the UI state (but keep the continents data separated). Something like: { '<randomId>': { Asia: { collapsed: true } } } IMHO this really makes things unnecessary complicated. It also makes it hard to re-use the ContinentsList component (maybe you want to use the same component in some other project which uses something else than redux?). I always try to think of the API of my react components first and how they later on can be easily reused. After I figured out the API of the react component then I might connect it to some store. Probably the ContinentsList component would somehow look like this: <ContinentsList continents={<continents data>} renderItem={({ collapse, isCollapsed, continent }) => { return <ContinentItem onCollapseClick={collapse} isCollapsed={isCollapsed} continent={continent} /> }} />
- baddox 9y agoI’m still having trouble imagining an application where you would need ContinentLists in different spots without some well-defined arrangement of them in the Redux state where each can be identified by something more concrete than a random ID.
- xatxat 9y agoYeah, I agree that this is an edge case (nothing that I run into on a daily base) and the ContinentLists example might not be the best in this context. Something like a Dashboard with dynamic widgets would probably be a more realistic case.