|
2 | 2 |
|
3 | 3 | NgRx is a framework for building reactive applications in Angular. NgRx provides state management, isolation of side effects, entity collection management, router bindings, code generation, and developer tools that enhance developers experience when building many different types of applications.
|
4 | 4 |
|
5 |
| -## Core Principles |
6 |
| - |
7 |
| -- State is a single, immutable data structure. |
8 |
| -- Components delegate responsibilities to side effects, which are handled in isolation. |
9 |
| -- Type-safety is promoted throughout the architecture with reliance on TypeScript's compiler for program correctness. |
10 |
| -- Actions and state are serializable to ensure state is predictably stored, rehydrated, and replayed. |
11 |
| -- Promotes the use of functional programming when building reactive applications. |
12 |
| -- Provide straightforward testing strategies for validation of functionality. |
| 5 | +## Why NgRx for State Management? |
| 6 | + |
| 7 | +NgRx provides state management for creating maintainable explicit applications, by storing single state and the use of actions in order to express state changes. |
| 8 | + |
| 9 | + |
| 10 | +### Serializability |
| 11 | + |
| 12 | +By normalizing state changes and pass them through observables, NgRx provides serializability and ensures state is predictably stored. This enables to save the state to an external storage, for example, `localStorage`. |
| 13 | + |
| 14 | +In addition, it also allows to inspect, download, upload, and dispatch actions, all from the [Store Devtools](guide/store-devtools). |
| 15 | + |
| 16 | + |
| 17 | +### Type Safety |
| 18 | +Type safety is promoted throughout the architecture with reliance on the TypeScript compiler for program correctness. |
| 19 | + |
| 20 | + |
| 21 | +### Encapsulation |
| 22 | + |
| 23 | +Using NgRx [Effects](guide/effects) and [Store](guide/store), any interaction with external resources side effects, like network requests, web socket and any business logic can be isolated from the UI. This isolation allows for more pure and simple components, and keep the single responsibility principle. |
| 24 | + |
| 25 | +### Testable |
| 26 | + |
| 27 | +Because [Store](guide/store) uses pure functions for changing state and selecting data from state, and the ability to isolate side effects from the UI, testing becomes very straightforward. |
| 28 | +NgRx also provides tests setup like `provideMockStore` and `provideMockActions` for isolated tests, and a better test experience. |
| 29 | + |
| 30 | +### Performance |
| 31 | + |
| 32 | +[Store](guide/store) is built on a single immutable data state, making change detection turn into a very easy task using an [`OnPush`](https://angular.io/api/core/ChangeDetectionStrategy#OnPush) strategy. |
| 33 | +NgRx is also powered by memoized selector functions which optimize state query computations. |
| 34 | + |
| 35 | +## When Should I Use NgRx for State Management |
| 36 | + |
| 37 | +In particular, you might use NgRx when you build an application with a lot of user interactions and multiple data sources, when managing state in services are no longer sufficient. |
| 38 | + |
| 39 | +A good substance that might answer the question "Do I need NgRx", is the |
| 40 | +<a href="https://youtu.be/omnwu_etHTY" target="_blank">**SHARI**</a> principle: |
| 41 | + |
| 42 | +* **S**hared: state that is accessed by many components and services. |
| 43 | + |
| 44 | +* **H**ydrated: state that is persisted and rehydrated from external storage. |
| 45 | + |
| 46 | +* **A**vailable: state that needs to be available when re-entering routes. |
| 47 | + |
| 48 | +* **R**etrieved: state that must be retrieved with a side-effect. |
| 49 | + |
| 50 | +* **I**mpacted: state that is impacted by actions from other sources. |
| 51 | + |
| 52 | + |
| 53 | +However, realizing that using NgRx comes with some tradeoffs is also crucial. It is not meant to be the shortest or quickest way to write code and encourage its users the usage of many files. |
| 54 | +It is also often require a steep learning curve, including some good understanding of [`RxJs`](https://rxjs-dev.firebaseapp.com/) and [`Redux`](https://redux.js.org/). |
| 55 | + |
13 | 56 |
|
14 | 57 | ## Packages
|
15 | 58 |
|
|
0 commit comments