Here's a cheat sheet for NgRx, a reactive state management library for Angular applications. This cheat sheet covers the basics, including actions, reducers, effects, selectors, and common patterns.
1. Actions
Define actions using createAction from @ngrx/store:
This cheat sheet covers the basics of NgRx. For more information, refer to the official documentation: https://ngrx.io/
Sure, here are some useful NgRx recipes to help you build robust and maintainable Angular applications:
1. Feature Modules and State Management
Create a feature module with its own state management, including actions, reducers, effects, and selectors. This helps keep your code organized and modular.
Recipe:
Create a feature module (e.g., UserModule).
Define actions, reducers, effects, and selectors in the feature module.
Import the feature module in the root module (AppModule).
Use StoreModule.forFeature() and EffectsModule.forFeature() to register the feature's reducer and effects.
2. Normalizing State
Normalize your state to avoid nested data structures and improve performance. Use the @ngrx/entity library to manage entities.
Recipe:
Define an entity adapter using createEntityAdapter() from @ngrx/entity.
Use the adapter's methods (e.g., addOne, updateOne, removeOne) in your reducers.
Create selectors using the adapter's getSelectors() method.
3. Memoized Selectors
Use memoized selectors to improve performance by caching the result of a selector until its dependencies change.
Recipe:
Create memoized selectors using createSelector() from @ngrx/store.
Ensure that the input selectors are pure functions.
4. Lazy Loading Feature Modules
Lazy load feature modules with their own state management.
Recipe:
Create a feature module with its own state management.
Use loadChildren in the router configuration to lazy load the feature module.
Register the feature's reducer and effects using StoreModule.forFeature() and EffectsModule.forFeature() in the feature module's forRoot() method.
5. Handling Server-Side Errors
Handle server-side errors gracefully and display appropriate messages to the user.
Recipe:
Define actions for handling errors (e.g., loadUsersFailure).
Use the catchError operator in effects to dispatch the error action.
Display error messages using a centralized error handling component or service.
6. Testing NgRx Components
Test NgRx components using the Store and provideMockStore() from @ngrx/store/testing.
Recipe:
Use provideMockStore() to provide a mock store in your tests.
Use the initialState property to set the initial state of the store.
Use the select method to test selectors.
Use the dispatch method to test actions.
7. NgRx Data
Use NgRx Data to simplify state management for CRUD operations.
Recipe:
Define an entity definition using EntityMetadataMap.
Use the EntityCollectionServiceBase to perform CRUD operations.
Customize the behavior using the EntityCollectionServiceElementsFactory.
These recipes should help you build robust and maintainable Angular applications using NgRx. For more information, refer to the official documentation: https://ngrx.io/