{"id":1535,"date":"2023-09-28T05:00:00","date_gmt":"2023-09-28T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1535"},"modified":"2023-09-22T16:01:21","modified_gmt":"2023-09-22T23:01:21","slug":"state-management-action-and-reducer","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/state-management-action-and-reducer\/","title":{"rendered":"State Management: Action and Reducer"},"content":{"rendered":"\n<p>Now that we&#8217;ve covered what&#8217;s the global <code><a href=\"https:\/\/www.ngxs.io\/concepts\/state\" target=\"_blank\" rel=\"noopener\" title=\"\">State<\/a><\/code> and what&#8217;s the <code><a href=\"https:\/\/www.ngxs.io\/concepts\/store\" target=\"_blank\" rel=\"noopener\" title=\"\">Store<\/a><\/code> service, let&#8217;s take a look at how we can update that <code>State<\/code>. In our <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/introduction-to-state-management\/\" target=\"_blank\" rel=\"noopener\" title=\"\">introduction to state management<\/a>, we introduced Actions.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.ngxs.io\/concepts\/actions\" target=\"_blank\" rel=\"noopener\" title=\"\">Actions<\/a> are simple objects with a name\/type describing what we&#8217;re trying to do. An optional payload contains the parameters for that Action. In our example of currency switcher, we want to change a currency, so we would create the following Action, where the payload is the new currency:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"146\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-48.png\" alt=\"\" class=\"wp-image-1539\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-48.png 602w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-48-300x73.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above Action does absolutely nothing on its own. We need a reducer to implement the corresponding state transition. With NgXs, a reducer is a method with the decorator <code>@Action<\/code> in our State class: <\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"792\" height=\"297\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-49.png\" alt=\"\" class=\"wp-image-1540\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-49.png 792w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-49-300x113.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-49-768x288.png 768w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>In the above code, we define which method runs when the Action of type <code>ChangeCurrency<\/code> is dispatched to our state management library.<\/p>\n\n\n\n<p>That method takes the current State, creates a copy of it (this is a <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/#:~:text=State%20changes%20are%20made%20with%20pure%20functions%20called%20reducers.%20These%20functions%20take%20the%20previous%20state%2C%20an%20action%2C%20and%20return%20the%20next%20state.%20They%20return%20new%20state%20objects%20instead%20of%20mutating%20the%20previous%20state.\" target=\"_blank\" rel=\"noopener\" title=\"\">core principle of Redux<\/a>), and then changes the <code>currency<\/code> and <code>exchangeRate<\/code> in the new state object.<\/p>\n\n\n\n<p>Then, NgXs will automatically notify any component\/service\/directive subscribed to that State that a new value has been set.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to dispatch an action?<\/h3>\n\n\n\n<p>If we want to use the Action and Reducer created earlier, we use the only object that knows about everything in our state-management machinery: The <a href=\"https:\/\/www.ngxs.io\/concepts\/store\" target=\"_blank\" rel=\"noopener\" title=\"\">Store<\/a>.<\/p>\n\n\n\n<p>Conveniently enough, our Store has a <code>dispatch<\/code> method that can be used for any Action:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"532\" height=\"33\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-50.png\" alt=\"\" class=\"wp-image-1541\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-50.png 532w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-50-300x19.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As a reminder, the Store is a service that can be injected anywhere we need it:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"399\" height=\"64\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-51.png\" alt=\"\" class=\"wp-image-1542\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-51.png 399w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-51-300x48.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>We&#8217;ve covered most of the pieces of our state management architecture. We know how to create a State, an Action, a Reducer and use the Store to dispatch an action and update our State:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"471\" height=\"245\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-52.png\" alt=\"\" class=\"wp-image-1543\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-52.png 471w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-52-300x156.png 300w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>In our next post in this series, we&#8217;ll see how to Select and subscribe to specific parts of our 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>Now that we&#8217;ve covered what&#8217;s the global State and what&#8217;s the Store service, let&#8217;s take a look at how we can update that State. In our introduction to state management, we introduced Actions. Actions are simple objects with a name\/type describing what we&#8217;re trying to do. An optional payload contains the parameters for that Action. [&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-1535","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\/1535","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=1535"}],"version-history":[{"count":5,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1535\/revisions"}],"predecessor-version":[{"id":1548,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1535\/revisions\/1548"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}