NgRx Entities: Updating the selectors

Note: This will be a series of posts around how I moved from custom built reducers and state management with NgRx to use the NgRx Entity library.


In NgRx Entities: Namespaces with TypeScript I previously wrote how I was changing up my reducers to use the Entity library from NgRx and utilizing TypeScript to my advantage. The next step was to update the selectors I had setup to use the new namespace and adapter.

A great reference book that goes deeper into this topic: Architecting Angular Applications with Redux, RxJS, and NgRx

What was there before

Again, still as an example with using books, here is a snippet code of what the selector file looked like.

1
2
3
4
5
6
7
8
9
10
11
12
13
// store/selectors/books.selectors.ts

const selectBooksState = (state: AppState) => state.books;

export const selectBooks = createSelector(
selectBooksState,
(state: BooksState) => state.books,
);

export const selectSelectedBook = createSelector(
selectBooksState,
(state: BooksState) => state.selectedBook,
);

Here we have two selectors:

  1. selectBooks which will return the full collection of the books in the store
  2. selectSelectedBook which returns the individual selected book from the store of books

Moving to the createFeatureSelector

Since the entity use with NgRx creates a more complex store object, moving to the createFeatureSelector needed to happen. This method from the NgRx store library creates a specific key on the store for your feature so that an optimization happens during the selection process.

1
2
3
4
5
// store/selectors/books.selectors.ts

export const selectBooksState
= createFeatureSelector<BooksStore.State>('books');

With this in place, the selector can access the store quicker based on the shape created by the entity setup:

1
2
3
4
5
6
7
8
9
10
11
12
// store shape
{
books: {
ids: [1, 2, ...],
entities: [
{ id: 1, title: 'The Shinning' },
{ id: 2, title: 'It' },
...
],
selectedBook: null,
}
}

Now I can change up the rest of the selectors to use the adapter selectors exported from the BooksStore that was exported and also change the selectSelectedBook to look into the entities array.

1
2
3
4
5
6
7
8
9
10
11
12
// store/selectors/books.selectors.ts

export const selectBooks = createSelector(
selectBooksState,
BooksStore.selectAll,
);

export const selectSelectedBook
= (bookId: number) => createSelector(
selectBooksState,
(state: BooksStore.State) => state.entities[bookId],
);

Again, there tends to be extra code with this move, but in the long run, the changes made to the reducers make up for it all, which I will dive into next.

Filed under: Code