-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Slices have been a great improvement in our code base for isolating all the previous patterns (actions / action creators, reducers, selectors) to slices. The last piece we are looking to isolate is selecting sub-state in a slice when using combineReducers in a tree structure. It seems that the slice's exposed selector expects the state to be the parent relative to that slice but in the connect function from react-redux we are given the rootState. With the rootState we have to know how to get to the sub-state where the slice lives.
I've read several thoughts on here about combineSlices which could be a good approach to solving this. The slice could then expose a function which given a rootState knows how to return the slice.
Our example state tree:
- rootState
- screens
- screen1
- screen2
- userName
- userEmail
so if we had a slice for screen2, the connect function on screen2 looks something like:
connect(
(rootState) => ({
userName: rootState.screens.screen2.userName
},
{
setUserName: slice.actions.setUsername,
},
)(screen2Component);
This means the component needs to know the structure of the state. Instead you could imagine something like:
connect(
slice.selector,
slice.reducers,
)(screen2Component);
or
connect(
(rootState) => ({
userName: slice.selector(rootState).userName
},
{
setUserName: slice.actions.setUsername,
},
)(screen2Component);
Any thoughts or ideas? I am definitely willing to help work on solving this. I want to minimize the amount of places that know the structure of the actual state. Leaving structure to combineSlices
alone seems to be the best bet.