Skip to content

Feature Discussion: Slice selector behavior with combineReducers #127

@nmastereal

Description

@nmastereal

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions