Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created January 8, 2021 15:28
Show Gist options
  • Save mindscratch/22d507f8401dc9f78627db8c61b3f931 to your computer and use it in GitHub Desktop.
Save mindscratch/22d507f8401dc9f78627db8c61b3f931 to your computer and use it in GitHub Desktop.
NgRX parameterized selector problem
import { State } from '../shared';
constructor(store: Store<State>) {
// this call to errors because I haven't passed a "state"
store.select(selectNotificationsByGroupType(), { type: this.group.type });
}
export interface State {
groups: NotificationGroup[];
error: string | null;
}
export const selectAll = createSelector(
(state: State) => state.groups,
(groups: NotificationGroup[]) => groups
);
// parameterized selector to get notifications by group type
export const selectNotificationsByGroupType = () =>
createSelector(
(state: State, props) =>
state.groups.find((group) => group.type === props.type),
(group) => (group ? group.notifications : [])
);
export const selectNotificationGroupsState = (state: State) => state.groups;
export const selectAllNotificationGroups = createSelector(
selectNotificationGroupsState,
fromNotifications.selectAll
);
export const selectNotificationsByGroupType = createSelector(
selectNotificationGroupsState,
fromNotifications.selectNotificationsByGroupType
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment