Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Discussion: Slice selector behavior with combineReducers #127

Closed
nmastereal opened this issue Mar 25, 2019 · 2 comments
Closed

Feature Discussion: Slice selector behavior with combineReducers #127

nmastereal opened this issue Mar 25, 2019 · 2 comments

Comments

@nmastereal
Copy link

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.

@markerikson
Copy link
Collaborator

I really don't have any good answers on this. This is basically the "encapsulated selectors" question, which Randy Coulman wrote about here and the other linked posts:

Solving Circular Dependencies in Modular Redux.

@markerikson
Copy link
Collaborator

Coming back around to this. I think the conclusion is that we're going to remove any slice-specific selector functionality, and leave it up to you to define whatever selectors you feel are appropriate, same as with any other Redux app. (See #91 for some related thoughts.)

markerikson pushed a commit that referenced this issue Apr 20, 2021
Co-authored-by: Matt Sutkowski <msutkowski@gmail.com>
markerikson pushed a commit that referenced this issue Apr 20, 2021
* docs: update comparison (#103)

Adds "declarative" as an option for API definition for React Query. With v3, you can now configure all of your queries and mutations up front before calling them.

* Add algolia config (#126)

* use authorization instead of authentication (#128)

* run tests on PRs

* Link directly to #redux on Reactiflux (#127)

Co-authored-by: Matt Sutkowski <msutkowski@gmail.com>

Co-authored-by: Tanner Linsley <tannerlinsley@gmail.com>
Co-authored-by: Matt Sutkowski <msutkowski@gmail.com>
Co-authored-by: Nick McCurdy <nick@nickmccurdy.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants