{"id":1510,"date":"2023-09-26T05:00:00","date_gmt":"2023-09-26T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1510"},"modified":"2023-09-22T16:01:08","modified_gmt":"2023-09-22T23:01:08","slug":"state-management-state-and-store","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/state-management-state-and-store\/","title":{"rendered":"State Management: State and Store"},"content":{"rendered":"\n<p>After <a href=\"http:\/\/State Management: Action and reducer\" target=\"_blank\" rel=\"noopener\" title=\"\">introducing state management<\/a> concepts last week, let&#8217;s dive deeper and get into two essential concepts of state management libraries: <a href=\"https:\/\/www.ngxs.io\/concepts\/store\" target=\"_blank\" rel=\"noopener\" title=\"\">Store<\/a> and <a href=\"https:\/\/www.ngxs.io\/concepts\/state\" target=\"_blank\" rel=\"noopener\" title=\"\">State<\/a>. I&#8217;m using <a href=\"https:\/\/www.ngxs.io\/\" target=\"_blank\" rel=\"noopener\" title=\"\">NgXs<\/a> for these examples because the library&#8217;s syntax is the most straightforward and consistent with how Angular works, using Typescript decorators to configure the different classes.<\/p>\n\n\n\n<p>The example we will be using is an <a href=\"https:\/\/store.angulartraining.com\/#\/\" target=\"_blank\" rel=\"noopener\" title=\"\">online store<\/a> where the user can change the currency and see the different prices get updated as a result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"325\" height=\"174\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-36.png\" alt=\"\" class=\"wp-image-1512\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-36.png 325w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-36-300x161.png 300w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>We want to manage that currency and the exchange rate that goes with it using state management. The first step is to create a piece of State that handles that data &#8211; and provide a name and default value for it:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"188\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-38.png\" alt=\"\" class=\"wp-image-1514\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-38.png 602w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-38-300x94.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above State has to be registered with the <code>NgxsModule<\/code> in our <code>AppModule<\/code> as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"534\" height=\"326\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-39.png\" alt=\"\" class=\"wp-image-1518\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-39.png 534w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-39-300x183.png 300w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And that&#8217;s it! Now, to interact with that State, we can use the <code><a href=\"https:\/\/www.ngxs.io\/v\/v3.2\/concepts\/store\" target=\"_blank\" rel=\"noopener\" title=\"\">Store<\/a><\/code> service and inject it wherever we need it:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"568\" height=\"153\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-40.png\" alt=\"\" class=\"wp-image-1519\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-40.png 568w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-40-300x81.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Of course, you can create multiple pieces of  State for your application using the same approach as above. Every new part of our State gets added as a property to the global <code>state<\/code> object. For instance, our current currency information can be found at <code>state.currencyInfo<\/code>. If we add a new <code>State<\/code> with a name set to <code>cartContents<\/code>, its data would be accessible under <code>state.cartContents<\/code>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In the next post of this series, we&#8217;ll see how to use <a href=\"https:\/\/www.ngxs.io\/concepts\/actions\" target=\"_blank\" rel=\"noopener\" title=\"\">Actions<\/a> to update the application&#8217;s State. You can see that <a href=\"https:\/\/stackblitz.com\/edit\/at-ngxs-demo\" target=\"_blank\" rel=\"noopener\" title=\"\">example in action on Stackblitz<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After introducing state management concepts last week, let&#8217;s dive deeper and get into two essential concepts of state management libraries: Store and State. I&#8217;m using NgXs for these examples because the library&#8217;s syntax is the most straightforward and consistent with how Angular works, using Typescript decorators to configure the different classes. The example we will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,37],"tags":[],"class_list":["post-1510","post","type-post","status-publish","format-standard","hentry","category-ngxs","category-state-management"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1510"}],"version-history":[{"count":7,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1510\/revisions"}],"predecessor-version":[{"id":1547,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1510\/revisions\/1547"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}