{"id":176,"date":"2019-09-18T09:20:39","date_gmt":"2019-09-18T09:20:39","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=176"},"modified":"2019-09-30T07:44:31","modified_gmt":"2019-09-30T07:44:31","slug":"ngrx-state-management-for-angular","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2019\/09\/18\/ngrx-state-management-for-angular\/","title":{"rendered":"NGRX &#8211; State management for Angular."},"content":{"rendered":"<p>Inspired by Redux, Store is RxJS powered state management for Angular applications. Store is a container for state of the application.<\/p>\n<blockquote><p>Refer to documentation at &#8211; <a href=\"https:\/\/ngrx.io\/guide\/store\">https:\/\/ngrx.io\/guide\/store<\/a><\/p><\/blockquote>\n<h2>Key components<\/h2>\n<p>I have a sample application posted at <a href=\"https:\/\/github.com\/samarthya\/free.time\">https:\/\/www.github.com<\/a><\/p>\n<h3>Actions<\/h3>\n<p>Unique events generated from components and services.<\/p>\n<pre>#v8 Onwards\r\nexport const login = createAction('[Login Initiatied] Login',\r\n\u00a0\u00a0props&lt;{ principal: IPrincipal }&gt;()\r\n)<\/pre>\n<div>\n<pre>#Older way of creating actions is still valid.\r\nimport\u00a0{\u00a0Action\u00a0}\u00a0from\u00a0'@ngrx\/store';\r\n\r\nexport\u00a0class\u00a0Login\u00a0implements\u00a0Action\u00a0{\r\n\u00a0\u00a0readonly\u00a0type\u00a0=\u00a0'[Login\u00a0Page]\u00a0Login';\r\n \u00a0constructor(public principal: IPrincipal) {}\r\n}<\/pre>\n<\/div>\n<p>You can dispatch an action like below<\/p>\n<div>\n<div>\n<pre>import\u00a0*\u00a0as\u00a0appActions\u00a0from\u00a0'..\/actions\/login.action';<\/pre>\n<\/div>\n<pre>principal = {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0email:\u00a0this.username.value,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0password:\u00a0this.password.value\r\n\u00a0\u00a0\u00a0\u00a0};\r\n\r\nthis.store.dispatch(appActions.login({ principal }));<\/pre>\n<\/div>\n<h4>Example Actions<\/h4>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter  wp-image-185\" src=\"https:\/\/www.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/Untitled-Diagram2-300x289.png\" alt=\"Action-Reducer-State\" width=\"555\" height=\"534\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/Untitled-Diagram2-300x289.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/Untitled-Diagram2-768x740.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/Untitled-Diagram2.png 771w\" sizes=\"(max-width: 555px) 100vw, 555px\" \/><\/p>\n<div>\n<pre>export\u00a0const\u00a0login\u00a0=\u00a0createAction('[Login\u00a0Initiatied]\u00a0Login',\r\n\u00a0\u00a0props&lt;{\u00a0principal:\u00a0IPrincipal\u00a0}&gt;()\r\n);\r\n\r\nexport\u00a0const\u00a0loginSuccess\u00a0=\u00a0createAction('[Login\u00a0Success]\u00a0Login',\r\n\u00a0\u00a0props&lt;{\u00a0userProfile:\u00a0IUserProfile\u00a0}&gt;()\r\n);\r\n\r\nexport\u00a0const\u00a0loginFailure\u00a0=\u00a0createAction('[Login\u00a0Failure]\u00a0Login',\r\n\u00a0\u00a0props&lt;{\u00a0message:\u00a0any\u00a0}&gt;()\r\n);\r\n\r\nexport\u00a0const\u00a0logout\u00a0=\u00a0createAction('[Logout\u00a0initiated]\u00a0logout', \r\nprops&lt;{\u00a0userName:\u00a0string}&gt;()\r\n);<\/pre>\n<\/div>\n<h3>Reducers<\/h3>\n<p>Pure functions that manipulate the state.<\/p>\n<div>\n<pre>const\u00a0loginReducer\u00a0=\r\n\u00a0\u00a0createReducer(\r\n\u00a0\u00a0\u00a0\u00a0null,\r\n\u00a0\u00a0\u00a0\u00a0on(LoginActions.login,\u00a0(state,\u00a0{\u00a0principal\u00a0})\u00a0=&gt;\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0loggedIn:\u00a0false,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0userProfile:\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0user:\u00a0principal,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0details:\u00a0null,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0profile:\u00a0null\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};\r\n\u00a0\u00a0\u00a0\u00a0}),\r\n.\r\n.\r\n.<\/pre>\n<h3>Selectors<\/h3>\n<p>Selectors are pure functions to select, derive, compose pieces of state.<\/p>\n<div>\n<pre>export\u00a0const\u00a0selectTheState\u00a0=\u00a0createSelector(selectState,\u00a0state\u00a0=&gt;\u00a0state);<\/pre>\n<figure id=\"attachment_177\" aria-describedby=\"caption-attachment-177\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/ngrx.io\/guide\/store\"><img decoding=\"async\" class=\"wp-image-177 size-large\" src=\"https:\/\/www.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/state-management-lifecycle-1024x576.png\" alt=\"Flow of state\" width=\"1024\" height=\"576\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/state-management-lifecycle-1024x576.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/state-management-lifecycle-300x169.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/state-management-lifecycle-768x432.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/09\/state-management-lifecycle.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-177\" class=\"wp-caption-text\">Flow of state<\/figcaption><\/figure>\n<\/div>\n<h2>Example<\/h2>\n<h3>Installation<\/h3>\n<p>In my sample application my pacakge.json looks something like this for NGRX specific libraries<\/p>\n<div>\n<pre>\"@ngrx\/effects\":\u00a0\"^8.2.0\",\r\n\"@ngrx\/router-store\": \"^8.2.0\",\r\n\"@ngrx\/store\": \"^8.2.0\",\r\n\"@ngrx\/store-devtools\": \"^8.2.0\",<\/pre>\n<\/div>\n<p class=\"prettyprint lang-sh\">One has to first install the basic store<\/p>\n<\/div>\n<pre><code class=\"animated fadeIn\"><span class=\"pln\">npm install <\/span><span class=\"lit\">@ngrx<\/span><span class=\"pun\">\/<\/span><span class=\"pln\">store <\/span><span class=\"pun\">--<\/span><span class=\"pln\">save<\/span><\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inspired by Redux, Store is RxJS powered state management for Angular applications. Store is a container for state of the application. Refer to documentation at &#8211; https:\/\/ngrx.io\/guide\/store Key components I have a sample application posted at https:\/\/www.github.com Actions Unique events generated from components and services. #v8 Onwards export const login = createAction(&#8216;[Login Initiatied] Login&#8217;, \u00a0\u00a0props&lt;{ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-176","post","type-post","status-publish","format-standard","hentry","category-personal"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=176"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}