Created
November 17, 2021 13:55
-
-
Save brandonroberts/84c1fa0b68652d53184f9f3c0099ae40 to your computer and use it in GitHub Desktop.
Create Feature 2 - NgRx 13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `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