{"id":1016,"date":"2023-05-31T05:00:00","date_gmt":"2023-05-31T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1016"},"modified":"2024-10-21T12:46:14","modified_gmt":"2024-10-21T19:46:14","slug":"signals-effect","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/signals-effect\/","title":{"rendered":"Signals: effect()"},"content":{"rendered":"\n<p>After talking about <a href=\"https:\/\/blog.angulartraining.com\/angular-signals-best-practices-around-exposing-signals-5385452150a1\" target=\"_blank\" rel=\"noreferrer noopener\">how to create Signals<\/a>, <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/three-ways-to-update-angular-signals\/\" target=\"_blank\" rel=\"noreferrer noopener\">how to update them<\/a>, and <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/signals-computed\" target=\"_blank\" rel=\"noopener\" title=\"\">how to derive a Signal value from other Signals<\/a>, let&#8217;s look at how we can register side effects out of any number of Signals.<\/p>\n\n\n\n<p>Enter <code><a href=\"https:\/\/angular.dev\/api\/core\/effect\" target=\"_blank\" rel=\"noopener\" title=\"\">effect()<\/a><\/code>. The behavior of <code>effect()<\/code> is almost the same as <code><a href=\"https:\/\/angular.dev\/api\/core\/computed\" target=\"_blank\" rel=\"noopener\" title=\"\">computed()<\/a><\/code>, with one major difference: <code>computed()<\/code> returns a new Signal, whereas <code>effect()<\/code> doesn&#8217;t return anything.<\/p>\n\n\n\n<p>As a result, <code>effect()<\/code> is suitable for debugging, logging information, or running some code that doesn&#8217;t need to update another Signal:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"722\" height=\"39\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-37.png\" alt=\"\" class=\"wp-image-1018\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-37.png 722w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-37-300x16.png 300w\" sizes=\"auto, (max-width: 722px) 100vw, 722px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can see that code in action <a href=\"https:\/\/stackblitz.com\/edit\/at-effect?file=src%2Fmain.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">here on Stackblitz<\/a>.<\/p>\n\n\n\n<p>Note that trying to update a Signal within an <code>effect()<\/code> is not allowed by default, as it could trigger unwanted behavior (infinite loop such as <code>SignalA<\/code> triggers update of <code>SignalB<\/code> that updates <code>SignalC<\/code> that updates <code>SignalA<\/code> &#8211; and around we go).<\/p>\n\n\n\n<p>That said, if you know what you&#8217;re doing and are 100% sure that you won&#8217;t trigger an infinite loop, you can override that setting with the optional parameter <code>allowSignalWrites<\/code> as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"122\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-38.png\" alt=\"\" class=\"wp-image-1019\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-38.png 663w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-38-300x55.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>After talking about how to create Signals, how to update them, and how to derive a Signal value from other Signals, let&#8217;s look at how we can register side effects out of any number of Signals. Enter effect(). The behavior of effect() is almost the same as computed(), with one major difference: computed() returns a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,29],"tags":[],"class_list":["post-1016","post","type-post","status-publish","format-standard","hentry","category-angular","category-signals"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1016","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=1016"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1016\/revisions"}],"predecessor-version":[{"id":2206,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1016\/revisions\/2206"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}