Skip to content

Instantly share code, notes, and snippets.

@brandonroberts
Created November 17, 2021 13:55
Show Gist options
  • Save brandonroberts/84c1fa0b68652d53184f9f3c0099ae40 to your computer and use it in GitHub Desktop.
Save brandonroberts/84c1fa0b68652d53184f9f3c0099ae40 to your computer and use it in GitHub Desktop.
Create Feature 2 - NgRx 13
// `createFeature` is imported from `@ngrx/store`
import { createFeature, createReducer } from "@ngrx/store";
import * as BookListPageActions from "./book-list-page.actions";
import * as BooksApiActions from "./books-api.actions";
interface State {
books: Book[];
loading: boolean;
}
const initialState: State = {
books: [],
loading: false,
};
// feature name and reducer are now passed to `createFeature`
export const booksFeature = createFeature({
name: "books",
reducer: createReducer(
initialState,
on(BookListPageActions.enter, (state) => ({
...state,
loading: true,
})),
on(BooksApiActions.loadBooksSuccess, (state, { books }) => ({
...state,
books,
loading: false,
}))
),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment