Skip to content

Commit

Permalink
docs(store): add example of createFeature extraSelectors (#3787)
Browse files Browse the repository at this point in the history
closes #3775
  • Loading branch information
Armenvardanyan95 authored Mar 16, 2023
1 parent cbdf524 commit bbd44e6
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions projects/ngrx.io/content/guide/store/feature-creators.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,47 @@ export const selectBookListPageViewModel = createSelector(
);
</code-example>

## Providing Extra Selectors

`createFeature` also can be used to provide extra selectors for the feature state with the `extraSelectors` option:

<code-example header="books.feature.ts">
import { createFeature, createReducer, on } from '@ngrx/store';
import { Book } from './book.model';

import * as BookListPageActions from './book-list-page.actions';

interface State {
books: Book[];
query: string;
}

const initialState: State = {
books: [],
query: '',
};

export const booksFeature = createFeature({
name: 'books',
reducer: createReducer(
initialState,
on(BookListPageActions.search, (state, action) => ({
...state,
query: action.query,
})),
),
extraSelectors: ({ selectQuery, selectBooks }) => ({
selectFilteredBooks: createSelector(
selectQuery,
selectBooks,
(query, books) => books.filter(book => book.title.includes(query)),
),
}),
});
</code-example>

The `extraSelectors` option accepts a function that takes the generated selectors as input arguments and returns an object of all the extra selectors. We can use it to define as many extra selectors as we need.

## Feature registration

Registering the feature reducer in the store can be done by passing the entire feature object to the `StoreModule.forFeature` method:
Expand Down

0 comments on commit bbd44e6

Please sign in to comment.