Updating a deeply nested state property: best practice #86
Replies: 1 comment 10 replies
-
|
Flattening the state structure might be good, but if you have multiple tables on a page, I wouldn't combine their states. I used to think Redux could be like a client-side database, but it's usually best to just fetch fresh data from an endpoint rather than worrying about precise updates in a normalized client state. Offline mode might be different. But this is somewhat beside the point of your question. If it's just convenience, you have to weight the pros and cons, such as losing the ability to expand state the way it probably expands in the UI, and instead having to match up IDs. The usual answer for nested state updates is interface AppState {
myObject: MyObjectType | null;
}
export const initialState: AppState = {
myObject: null;
};
export const adapter = joinAdapters<AppState>()({
myObject: createAdapter<MyObjectType | null>()({}),
})();This will create You could also create the inner adapter above to keep all the code organized by type. Deeply Nested ArraysAt this moment I have no idea how to handle this. Normally I need to come back to this. This might be the first scenario I've seen that turns out to really benefit from Immer over joinAdapters. But I need to think about it. In the meantime, take a look at One other pattern that might help is to create an array adapter creator: export function createArrayAdapter<T>() {
return createAdapter<T[]>()({
// ... reducers for arrays
});
}This is something StateAdapt should have soon. There's just been the entity adapter, but I imagine the issues with that would be worse with this. See this discussion: #81 But even with this many nested arrays, that might be awkward too. The state change function might be export const adapter = joinAdapters<State>()({
sections: sectionsAdapter,
})({
setCellValue: sectionsAdapter.setSubSectionsRowsCellsOneValue,
})();(Again see But if you're not doing much else, this is a lot of work for just one state change function. But the other issue would be the payload. The array adapter will automatically handle finding by id, so that needs to be separate from the payload. In the entity adapter, the overall action payload is like What are your thoughts on how Immer could be incorporated into StateAdapt? I'd prefer |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am curious how others would approach this problem. Say you have a property of your store that is a complex object (represented to the user as a dynamic form):
example adapter:
It can be quite a complex bit of code to update the value of a "cell" or add a row to a subSection inside of myObject. I usually use immer's produce to accomplish this task. I am wondering if state adapt has something built in for this task.
Secondly, would it be better to have a store that "normalizes" the deep object into top level properties of sections, subsections, rows and use id's to compose the object back together when needed?
so the state for the object would look like:
and I would write a transform function/reducer to return the data in the nested structure:
Just looking for opinions on best practices in this scenario.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions