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

NgRx Store: Add createChildSelectors function #2974

Closed
markostanimirovic opened this issue Mar 15, 2021 · 5 comments · Fixed by #3033
Closed

NgRx Store: Add createChildSelectors function #2974

markostanimirovic opened this issue Mar 15, 2021 · 5 comments · Fixed by #3033

Comments

@markostanimirovic
Copy link
Member

Description

createChildSelectors function creates memoized and strongly typed child selectors based on the parent selector. There are two signatures:

First:

export const selectUserState = createFeatureSelector<fromUsers.State>('users');

export const {
  selectUsers,
  selectLoading,
  selectPagination,
} = createChildSelectors(selectUserState, fromUsers.initialState);

Second:

export const {
  selectUsers,
  selectLoading,
  selectPagination,
} = createChildSelectors(selectUserState, ['users', 'loading', 'pagination']);

Full description is available here: https://github.com/markostanimirovic/ngrx-child-selectors

Other information:

This feature can be implemented when we upgrade TypeScript version to 4.1 or higher.

If accepted, I would be willing to submit a PR for this feature

[x] Yes ❤️
[ ] No

@brandonroberts
Copy link
Member

brandonroberts commented Mar 31, 2021

I think we should look at unifying this into something similar to createSlice in Redux Toolkit where you get the reducer key, reducer, the feature selector, and those child selectors for a given slice of state.

@markostanimirovic
Copy link
Member Author

Hey @brandonroberts,

Did you mean something like this?

const {
  selectMusiciansState // 👈 feature selector
  selectMusicians,
  selectLoading,
  selectPagination
} = createSliceSelectors('musicians', initialState);

@brandonroberts
Copy link
Member

brandonroberts commented Apr 30, 2021

Something more like

export const {
  reducer(maybe),
  featureKey,<- musicians
  featureSelector, <- createFeatureSelector('musicians') or selectMusiciansState
  selectLoading,
  selectCurrentMusicianId,
 ...
} = createFeature<State>({
  name: 'musicians',
  reducer
})

@markostanimirovic
Copy link
Member Author

Looks good 👍

I will be happy to create a PR if you and the other team members agree :)

Btw, I would also add createChildSelectors function, because it can be used within createFeature function, but also independently, to create nested selectors for any selector.

@david-shortman
Copy link
Contributor

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants