22. Use Entity Adapter
In this section, we will normalise our state into dictionaries to make it easier to manage, query and project our state. This is one of the most important steps to make better redux.
1. npm install @ngrx/entity
npm i @ngrx/entity2. Make an entity adapter
<---------- ABBREVIATED CODE SNIPPET ---------->
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { AttendeesActions, AttendeesActionTypes } from './attendees.actions';
import { Attendee } from '../../../models';
export interface State extends EntityState<Attendee> {
loading: boolean;
error: any;
}
const adapter: EntityAdapter<Attendee> = createEntityAdapter<Attendee>();
export const intitalState: State = adapter.getInitialState({
loading: false,
error: null
});
export function reducer(state = intitalState, action: AttendeesActions): State {
<---------- ABBREVIATED CODE SNIPPET ---------->
3. Update reducer to use entity adapter collection methods
3. Update selectors

StackBlitz Link
Last updated