Skip to content

Instantly share code, notes, and snippets.

@webmasterdevlin
Last active March 14, 2019 23:56
Show Gist options
  • Save webmasterdevlin/344fccf6831df698f85cf813402f7280 to your computer and use it in GitHub Desktop.
Save webmasterdevlin/344fccf6831df698f85cf813402f7280 to your computer and use it in GitHub Desktop.
NgRx Selectors and Store : src/app/reducers/index.ts
import { ActionReducerMap, createSelector, MetaReducer } from "@ngrx/store";
import { environment } from "../../environments/environment";
import { heroReducer, HeroState } from "./hero.reducer";
import { villainReducer, VillainState } from "./villain.reducer";
export interface State {
heroes: HeroState;
villains: VillainState;
}
/*
This reducers combine the two reducers which is the heroReducer and the villainReducer
*/
export const reducers: ActionReducerMap<State> = {
heroes: heroReducer,
villains: villainReducer
};
export const metaReducers: MetaReducer<State>[] = !environment.production
? []
: [];
// selector, selecting the state.heroes
export const selectHeroesState = (state: State) => state.heroes;
export const selectHero = createSelector(
selectHeroesState,
(state: HeroState) => state.heroes
);
// selector, selecting the state.villains
export const selectVillainsState = (state: State) => state.villains;
export const selectVillain = createSelector(
selectVillainsState,
(state: VillainState) => state.villains
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment