{"id":1575,"date":"2023-10-04T05:00:00","date_gmt":"2023-10-04T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1575"},"modified":"2023-09-27T11:28:29","modified_gmt":"2023-09-27T18:28:29","slug":"state-management-effect","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/state-management-effect\/","title":{"rendered":"State Management: Effect"},"content":{"rendered":"\n<p>Let&#8217;s get back to our series on <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/category\/state-management\/\" target=\"_blank\" rel=\"noopener\" title=\"\">state management<\/a> with this post. So far, we covered that state management libraries give us a global State that gets updated using Actions and Reducers. I haven&#8217;t told you yet that typical Redux Reducer functions are meant to be synchronous. In other words, you&#8217;re not supposed to make an HTTP request or anything asynchronous in a reducer.<\/p>\n\n\n\n<p>So, how do we implement asynchronous tasks? This is known as a <a href=\"https:\/\/redux.js.org\/usage\/side-effects-approaches#side-effects-overview\" target=\"_blank\" rel=\"noopener\" title=\"\">side-effect<\/a> (because it involves a third party such as a server) or just Effect.<\/p>\n\n\n\n<p>When we use Effects, the idea is to have an Action that triggers the Effect, and once the Effect completes, another Action is triggered:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"942\" height=\"238\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-68.png\" alt=\"\" class=\"wp-image-1577\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-68.png 942w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-68-300x76.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-68-768x194.png 768w\" sizes=\"auto, (max-width: 942px) 100vw, 942px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>For instance, in our current switcher example, we could have an Action called <code>LOAD_EXCHANGE_RATES<\/code>. This Action would trigger an Effect to make an HTTP request and fetch exchange rates from an API. For instance, the original Action would also update the State to make it a &#8220;loading&#8221; state.<\/p>\n\n\n\n<p>Then, once the HTTP request completes, the Effect would dispatch another action, <code>EXCHANGE_RATES_LOADED<\/code> and pass the data as its payload so it gets stored in the State. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Effects  are implemented differently based on the state management library<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>NgRx<\/strong><\/li>\n<\/ol>\n\n\n\n<p><a href=\"https:\/\/ngrx.io\/guide\/effects\" target=\"_blank\" rel=\"noopener\" title=\"\">NgRx effects<\/a> rely heavily on RxJs and look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"777\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-71-1024x777.png\" alt=\"\" class=\"wp-image-1582\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-71-1024x777.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-71-300x228.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-71-768x582.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-71-1536x1165.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-71.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>As you can see, the Effect code is verbose, but every single step makes sense: We turn an action into an asynchronous request that dispatches other Actions upon success or failure.<\/p>\n\n\n\n<p>If you want to learn more about NgRx, I have this <a href=\"https:\/\/www.linkedin.com\/learning\/learning-ngrx\/set-up-effects-for-asynchronous-tasks\" target=\"_blank\" rel=\"noopener\" title=\"\">video course on LinkedIn Learning<\/a>.<\/p>\n\n\n\n<p><strong>2. NgXs<\/strong><\/p>\n\n\n\n<p>NgXs has a different approach and doesn&#8217;t have effects. Instead, all actions <a href=\"https:\/\/www.ngxs.io\/advanced\/actions-life-cycle#theory\" target=\"_blank\" rel=\"noopener\" title=\"\">have four states by default<\/a>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"364\" height=\"475\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-72.png\" alt=\"\" class=\"wp-image-1583\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-72.png 364w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-72-230x300.png 230w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As a result, action handlers (reducers) can use asynchronous requests without needing any effects:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"455\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-73-1024x455.png\" alt=\"\" class=\"wp-image-1584\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-73-1024x455.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-73-300x133.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-73-768x341.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-73-1536x683.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/09\/image-73.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>As you can see, even if the Redux concepts remain the same, every state management library has a different approach. I like NgXs because of its simplicity. The code is more to the point and less reliant on <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/category\/rxjs\/operators\/\" target=\"_blank\" rel=\"noopener\" title=\"\">RxJs operators<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s get back to our series on state management with this post. So far, we covered that state management libraries give us a global State that gets updated using Actions and Reducers. I haven&#8217;t told you yet that typical Redux Reducer functions are meant to be synchronous. In other words, you&#8217;re not supposed to make [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-1575","post","type-post","status-publish","format-standard","hentry","category-state-management"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1575","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=1575"}],"version-history":[{"count":5,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1575\/revisions"}],"predecessor-version":[{"id":1586,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1575\/revisions\/1586"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}