{"id":5722,"date":"2020-01-31T11:09:21","date_gmt":"2020-01-31T10:09:21","guid":{"rendered":"http:\/\/blog.via-internet.de\/?p=5722"},"modified":"2020-01-31T11:09:21","modified_gmt":"2020-01-31T10:09:21","slug":"angular_working-with-state-management","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2020\/01\/31\/angular_working-with-state-management\/","title":{"rendered":"Angular | Working with State Management"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One of the most challenging things in software development is state management. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Motivation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">What is a state (or application state)? Theoretically, it is the entire memory of the application, but, typically, it is the data received via API calls, user inputs, presentation UI State, app preferences, etc. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is the <strong>data<\/strong> that can differentiate two instances of the same application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One example of application state would be a list of customers or products maintained in an application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Problem (we&#8217;re trying to solve)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Think of an application using  a list of data (products, customers, &#8230;). This list is the state that we are trying to manage. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Some API calls and user inputs could change the state ( i.e. the list ) by adding or removing items\/entries. The state change should be reflected in the UI and other dependent components. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We could solve this with a global variable to hold the list and then add\/remove customers from\/to it and then write the code to update the UI and dependencies. But, there are many pitfalls in that design which are not the focus of this article. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Solutions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Currently there are several state management libraries for Angular apps: <a href=\"https:\/\/ngrx.io\/\">NGRX<\/a>, <a href=\"https:\/\/www.ngxs.io\/\">NGXS<\/a> or <a href=\"https:\/\/netbasal.gitbook.io\/akita\/\">Akita<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/rxjs-dev.firebaseapp.com\/\">RxJS &#8211; Reactive Extensions Library for JavaScript<\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code. This project is a rewrite of Reactive-Extensions\/RxJS with better performance, better modularity, better debuggable call stacks, while staying mostly backwards compatible, with some breaking changes that reduce the API surface<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/blog.via-internet.de\/en\/blog\/2020\/01\/31\/working-with-state-management-and-angular\/?preview=true\">Vorschau(\u00f6ffnet in neuem Tab)<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"ngrxstore\"><a href=\"https:\/\/ngrx.io\/guide\/store\">@ngrx\/store<\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Store is RxJS powered state management for Angular applications, inspired by Redux. Store is a controlled state container designed to help write performant, consistent applications on top of Angular.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key concepts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/ngrx.io\/guide\/store\/actions\">Actions<\/a>&nbsp;describe unique events that are dispatched from components and services.<\/li><li>State changes are handled by pure functions called&nbsp;<a href=\"https:\/\/ngrx.io\/guide\/store\/reducers\">reducers<\/a>&nbsp;that take the current state and the latest action to compute a new state.<\/li><li><a href=\"https:\/\/ngrx.io\/guide\/store\/selectors\">Selectors<\/a>&nbsp;are pure functions used to select, derive and compose pieces of state.<\/li><li>State is accessed with the&nbsp;<code><a href=\"https:\/\/ngrx.io\/api\/store\/Store\">Store<\/a><\/code>, an observable of state and an observer of actions<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/ngrx.io\/generated\/images\/guide\/store\/state-management-lifecycle.png\" alt=\"NgRx State Management Lifecycle Diagram\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"f5f4\"><a href=\"https:\/\/netbasal.gitbook.io\/akita\/\">Akita<\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Akita is a state management pattern, built on top of RxJS, which takes the idea of multiple data stores from Flux and the immutable updates from Redux, along with the concept of streaming data, to create the&nbsp;<strong>Observable Data Stores model<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Akita<\/strong> encourages simplicity. It saves you the hassle of creating boilerplate code and offers powerful tools with a moderate learning curve, suitable for both experienced and inexperienced developers alike.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Akita<\/strong> is based on object-oriented design principles instead of functional programming, so developers with OOP experience should feel right at home. Its opinionated structure provides your team with a fixed pattern that cannot be deviated from.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"720\" src=\"https:\/\/i1.wp.com\/blog.via-internet.de\/wp-content\/uploads\/2020\/01\/akita-arcitecture.jpg?fit=700%2C394&amp;ssl=1\" alt=\"\" class=\"wp-image-5740\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/akita-arcitecture.jpg 1280w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/akita-arcitecture-300x169.jpg 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/akita-arcitecture-1024x576.jpg 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/akita-arcitecture-768x432.jpg 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/akita-arcitecture-1110x624.jpg 1110w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2020\/01\/akita-arcitecture-528x297.jpg 528w\" sizes=\"auto, (max-width: 1280px) 100vw, 1280px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"what-is-ngxs\"><a href=\"https:\/\/www.ngxs.io\/\">NGXS &#8211; State management pattern + library for Angular<\/a><a href=\"#what-is-ngxs\"><\/a><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">NGXS is a state management pattern + library for Angular. It acts as a single source of truth for your application&#8217;s state, providing simple rules for predictable state mutations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">NGXS is modeled after the CQRS pattern popularly implemented in libraries like Redux and NgRx but reduces boilerplate by using modern TypeScript features such as classes and decorators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction (taken from <a href=\"https:\/\/blog.strongbrew.io\/managing-state-in-angular\/\">here<\/a>)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-state\">What is state?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">State is basically everything that will define the UI that our user will be using. State could be whether a button should be visible or not, it could be the result of that button click and it could also be an&nbsp;<code>Array<\/code>&nbsp;of users that is coming from an API. State can live in different places throughout our entire application. Some state is very specific to a certain component where other state might be shared in different parts of our application. One piece of state could be a singleton instance, where a another piece of state could share the limited lifespan of a component that can be destroyed at any time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This big variety of what state could be, how long it lives and where it comes from results in complexity that we need to manage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-state-management\">What is state management?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">State management is the concept of adding, updating, removing and reading pieces of state in an application. When we have deeply nested data structures and we want to update a specific part deep down in the tree, it might become complex. In that case we have state management libraries that contain a&nbsp;<code>Store<\/code>&nbsp;which helps us with state management to get rid of that complexity. A quick note, we have to be careful that these libraries don\u2019t add complexity by overusing them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"reactive-state\">Reactive state<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Combining state management together with reactive programming can be a really nice way to develop single-page-applications. Whether our focus lies on Angular, Vue or React, combining these two principles will result in more predictable applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now what has state to do with reactive programming? A piece of state can change over time, so in a way we are waiting for new state changes. That makes it asynchronous.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s take this example for instance:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ false------true-----false---true...\nsidebarCollapsed$ = this.state.sidebarCollapsed$<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>sidebarCollapsed$<\/code>&nbsp;stream starts out with&nbsp;<code>false<\/code>, later on it becomes&nbsp;<code>true<\/code>&nbsp;and so on. This stream keeps on living. In Angular this state can be consumed with the&nbsp;<a href=\"https:\/\/angular.io\/api\/common\/AsyncPipe\">async pipe<\/a>&nbsp;as easy as:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;my-awesome-sidebar *ngIf=\"sidebarCollapsed$|async\">\n&lt;\/my-awesome-sidebar>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>async<\/code>&nbsp;pipe will subscribe to the&nbsp;<code>sidebarCollapsed$<\/code>&nbsp;pass it to the component, mark it for check and will automatically unsubscribe when the component gets destroyed. Keeping state in an observer pattern is nice because we can subscribe to the changes. Oh, and did I mention it plays super nice with Angular?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can either use a&nbsp;<code>BehaviorSubject<\/code>&nbsp;or state management frameworks that support Observables. Here are some really great ones with Observable support:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/ngrx.io\/\">Ngrx<\/a><\/li><li><a href=\"https:\/\/netbasal.gitbook.io\/akita\/\">Akita<\/a><\/li><li><a href=\"https:\/\/github.com\/ngxs\/store\">Ngxs<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"immutability-and-unidirectional-data-flow\">Immutability and Unidirectional data flow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before we dive deeper in state, there are 2 important principles that we should follow when managing state. The first principle is&nbsp;<strong>immutability<\/strong>, which means that we should never mutate data directly without creating a new reference of that object. If we mutate data directly, our application becomes unpredictable and it\u2019s really hard to trace bugs. When we work in an immutable fashion we can also take advantage of performance strategies like the&nbsp;<a href=\"https:\/\/netbasal.com\/a-comprehensive-guide-to-angular-onpush-change-detection-strategy-5bac493074a4\">ChangeDetection.OnPush<\/a>&nbsp;from Angular or React its&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/react-api.html#reactpurecomponent\">PureComponent<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When we use typescript we can enforce the typescript compiler to complain when we mutate data<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">type Foo = {\n    readonly bar: string; \n    readonly baz: number; \n}\n\nlet first = {bar: 'test', baz: 1};\nfirst.bar = 'test2'; \/\/ compilation error\nfirst = {...first, bar: 'test2'}; \/\/ success<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the previous example we have overwritten the&nbsp;<code>first<\/code>&nbsp;instance with an entire new instance that has an updated&nbsp;<code>bar<\/code>&nbsp;property.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays can be handled like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let arr = ['Brecht', 'Kwinten'];\narr.push('John'); \/\/ BAD: arr is mutated\narr = [...arr, 'John']; \/\/ Good, arr gets new reference<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">the&nbsp;<code>Array<\/code>&nbsp;prototype also has some great helper functions that we can use to enforce immutability like&nbsp;<code>map()<\/code>&nbsp;and&nbsp;<code>filter()<\/code>&nbsp;but this is not in scope for this article.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The second principle is&nbsp;<strong>Unidirectional data flow<\/strong>. In a nutshell, this means that we should never use two-way data binding on state. It is the absolute owner of that specific piece of state that is in charge of updating it (immutable of course).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both of these principles are highly enforced by the&nbsp;<a href=\"https:\/\/redux.js.org\/\">Redux<\/a>&nbsp;pattern.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-kind-of-states-are-there\">What kind of states are there?<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"router-state\">Router state<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Often forgotten, but one of the most important pieces of state a web application can have. Putting state in the route gives us the following advantages:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We can use the browser navigation buttons<\/li><li>We can bookmark the state<\/li><li>We can can copy and paste the url with the state to other users<\/li><li>We don\u2019t have to manage it, it\u2019s always there in the route<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Tip<\/strong>: Instead of handling modals with a&nbsp;<code>userDetailModalVisible<\/code>&nbsp;property, why not enjoy all the benefits mentioned above and bind it to a&nbsp;<code>users\/:userId<\/code>&nbsp;route? Using a child&nbsp;<code>router-outlet<\/code>&nbsp;in Angular makes this a piece of cake as we can see in this snippet.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;table>\n&lt;!--contains users -->\n&lt;\/table>\n&lt;router-outlet>\n&lt;!-- user detail modal rendered in here -->\n&lt;\/router-outlet>\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"component-state\">Component state<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Every component could contain state. That state could be shared with its dumb components or could be used in the component itself. Eg: When an&nbsp;<code>ItemComponent<\/code>&nbsp;has a property&nbsp;<code>selectedItems<\/code>&nbsp;which is an array of ids, and that array is never used in other components (that aren\u2019t children of that component), we can consider it component state. It belongs to that component, therefore the component should be responsible for it. Child components can consume that state but should&nbsp;<em>never mutate it<\/em>. Those components can notify their parent that is responsible for it, which could update it in an immutable way. For more information about smart and dumb components&nbsp;<a href=\"https:\/\/blog.strongbrew.io\/components-demystified\/#smart-vs-dumb-components\">look here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Personally, I try to avoid state management frameworks for managing component state because it\u2019s the responsibility of that component to manage that state. There are however good reasons to use state management frameworks to manage component state:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>When the state management is very complex<\/li><li>If we want to do&nbsp;<a href=\"https:\/\/blog.strongbrew.io\/Cancellable-optimistic-updates-in-Angular2-and-Redux\/\">optimistic updates<\/a><\/li><li>If we want to use it for&nbsp;<a href=\"https:\/\/blog.strongbrew.io\/How-we-made-our-app-real-time-in-6-lines-of-code\/\">realtime stuff<\/a><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If the state management of the component becomes a bit too complex and we don\u2019t want to use a state management framework just yet, we could use a state reducer in the component itself.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"persisted-state\">Persisted state<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Persisted state, is state that is being remembered when the user navigates between different pages. This could be whether a sidebar was collapsed or not, or when the user returns to a grid with a lot of filters and he wants them to be remembered and reapplied when he returns. Another example is a wizard with different steps, and every step needs to be persisted so the user can navigate back and forth and the last page is a result of all these steps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Persisted state is the type of state where we typically use a state management framework for, that being said, if we don\u2019t want to rely on an external dependency we can also manage it in a Angular&nbsp;<code>service<\/code>&nbsp;which can be a singleton that is shared throughout the entire application. If that&nbsp;<code>service<\/code>&nbsp;becomes too complex or there is a lot of state to manage, I would consider to put that state into a state management framework.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"shared-state\">Shared state<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When we are talking about shared state, we are talking about state that needs to be shared between different parts of our application. State that is being shared throughout different smart components. This means that the instance of this piece of state should live on a higher level, than the components that want to consume it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Shared state can be managed in a state management framework like&nbsp;<a href=\"https:\/\/redux.js.org\/\">Redux<\/a>,&nbsp;<a href=\"https:\/\/ngrx.io\/\">Ngrx<\/a>,&nbsp;<a href=\"https:\/\/netbasal.gitbook.io\/akita\/\">Akita<\/a>,&nbsp;<a href=\"https:\/\/github.com\/ngxs\/store\">Ngxs<\/a>&nbsp;and so on, but if that state is small and simple we can also manage it manually. Let\u2019s say that we want an&nbsp;<code>Observable<\/code>&nbsp;of an&nbsp;<code>Array<\/code>&nbsp;of countries that we need to share throughout the entire application. In Angular we could have a&nbsp;<code>CountryService<\/code>&nbsp;that fetches the countries from the API once, and then shares it throughout the entire application. For that we can use the&nbsp;<code>shareReplay<\/code>&nbsp;operator from RxJS.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export class CountryService {\n    ...\n    countries$ = this.httpClient.get('countries').pipe(shareReplay(1));\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Simple right, one line of code?! For this we don\u2019t&nbsp;<strong>need<\/strong>&nbsp;a state management framework, although it can also have its benefits. Some developers like to keep all their master data in a&nbsp;<code>Redux<\/code>&nbsp;store, and that\u2019s fine. Just know that we don\u2019t have to. I like to develop by the&nbsp;<strong>KISS<\/strong>&nbsp;principle (<strong>K<\/strong>eep&nbsp;<strong>I<\/strong>t&nbsp;<strong>S<\/strong>imple&nbsp;<strong>S<\/strong>tupid) as much as possible, so I favor this approach many times. Think about the amount of lines of code we saved by this approach. Beware that every line of code we write, not only needs to be written but also maintained.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"which-state-needs-to-be-managed\">Which state needs to be managed?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we know what state is, we have to ask ourselves which state needs to be managed, and where do we manage that state? In a component, singleton service or a framework (Store)?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is the part where the strong opinions surface. I would suggest to use what works for you and your team and really think about, but here are&nbsp;<strong>my personal opinionated<\/strong>&nbsp;guidelines:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I try to avoid state management frameworks where possible. RxJS already leverages us with a lot already and I like to think&nbsp;<strong>KISS<\/strong>.<\/li><li>I try to avoid using state management frameworks to communicate with different parts in my application, I believe state is unrelated to communication.<\/li><li>When my component can handle the state and it\u2019s not too complex, I let my component in charge of managing that state.<\/li><li>Master data like countries are exposed in a service which uses the&nbsp;<code>shareReplay<\/code>&nbsp;operator.<\/li><li>I don\u2019t put the result of a&nbsp;<code>getById<\/code>&nbsp;API call into a store if there is no one consuming that state except for the component requesting it<\/li><li>I use a facade between my smart components and my store\/services to make refactoring easier in the future.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">However, there is also a popular opinion out there to put literally everything in the store which has the following advantages:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We can see the flow of the code in devtools<\/li><li>Consistent pattern<\/li><li>We can leverage selectors with memoization<\/li><li>Easier for realtime applications<\/li><li>Optimistic updates are easier<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">However, there are a few downsides as well:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A gigantic amount of bloat code: Bigger bundle size, more maintenance and dev time. Eg: If we would use the complete Ngrx pattern for the&nbsp;<code>countries$<\/code>&nbsp;example we would have to write an:&nbsp;<code>action<\/code>,&nbsp;<code>actiontype<\/code>,&nbsp;<code>effect<\/code>&nbsp;and a&nbsp;<code>reducer<\/code>.<\/li><li>Tightly coupled to a strong dependency that is hard to get rid of in the future<\/li><li>Generally more complex<\/li><li>The user his screen can get out of sync with the backend<\/li><li>Cache invalidation: if we add a&nbsp;<code>currentUserToEdit<\/code>&nbsp;in the store, we have to get it out when we navigate away<\/li><li>We can\u2019t use the&nbsp;<code>async<\/code>&nbsp;pipe to cancel pending XHR requests<\/li><li>We create a distributed monolith of some sort<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">More to read<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/netbasal.com\/introducing-akita-a-new-state-management-pattern-for-angular-applications-f2f0fab5a8\">Introducing Akita: A New&nbsp;State Management Pattern for Angular Applications<\/a><\/li><li><a href=\"https:\/\/dzone.com\/articles\/angular-app-state-management-with-ngrx\">Angular Tutorial: State Management With NgRx<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction One of the most challenging things in software development is state management. Motivation What is a state (or application state)? Theoretically, it is the entire memory of the application, but, typically, it is the data received via API calls, user inputs, presentation UI State, app preferences, etc. It is the data that can differentiate two instances of the same application. One example of application state would be a list of customers or products maintained in an application. Problem (we&#8217;re trying to solve) Think of an application using a list of data (products, customers, &#8230;). This list is the state that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[90,69],"tags":[],"class_list":["post-5722","post","type-post","status-publish","format-standard","hentry","category-angular","category-rxjs"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/5722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=5722"}],"version-history":[{"count":0,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/5722\/revisions"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=5722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=5722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=5722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}