<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lourens Schep on Medium]]></title>
        <description><![CDATA[Stories by Lourens Schep on Medium]]></description>
        <link>https://medium.com/@lourensschep?source=rss-2506ca10717d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3wxWaIeDZv3VmjBy-38ddQ.jpeg</url>
            <title>Stories by Lourens Schep on Medium</title>
            <link>https://medium.com/@lourensschep?source=rss-2506ca10717d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 10:10:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lourensschep/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Get the Most Out of NgRx]]></title>
            <link>https://medium.com/well-red/how-to-get-the-most-out-of-ngrx-96a971380463?source=rss-2506ca10717d------2</link>
            <guid isPermaLink="false">https://medium.com/p/96a971380463</guid>
            <category><![CDATA[ngrx]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Lourens Schep]]></dc:creator>
            <pubDate>Thu, 25 Oct 2018 14:18:44 GMT</pubDate>
            <atom:updated>2018-10-25T14:21:15.837Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to get the most out of NgRx</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LnJYso-8a3Vmtjfqf8yO7A.png" /></figure><p>A common question developers face when creating large-scale web apps is, “How can we maintain state across the entire app”? NgRx answers this by having a single immutable state that can be updated and accessed through a global store service. The store allows us to dispatch actions and select the data we want. Selecting the data returns RxJs observables so we can be notified of changes.</p><p>NgRx has multiple parts in place to accomplish this: actions, reducers, effects, and selectors. Each of these parts has a big role to play, but can sometimes be seen as unnecessary if not used to their full extent.</p><p>Below are some ways we can make better use of NgRx, allowing us to keep our code separated and our components clean.</p><h3>Selectors</h3><p>Selectors in NgRx are a great way to get specific pieces of data from the state. These selectors can be chained and combined together.</p><p>Combining selectors is an easy way to make the data dynamic and minimize the amount of logic we need in our components.</p><p><strong>Using the router state</strong></p><p>One great example of combining selectors is to make use of the <a href="https://github.com/ngrx/platform/blob/master/docs/router-store/README.md">StoreRouterConnectingModule</a>. By default, this gives us access to the current URL, params, and query params. We can add more info to the router state if necessary by creating a custom router state serializer. We can use the router info within our selectors, using the params passed into the route to return the item that matches it.</p><p>Suppose we have a web app that allows us to view and edit specific blogs that have a URL layout like this:</p><pre>blogs/:blogId<br>blogs/:blogId/edit</pre><p>We can now create a specific selector to retrieve that specific blog by doing something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d05cd407e57a2309640f3b8ba203fcb5/href">https://medium.com/media/d05cd407e57a2309640f3b8ba203fcb5/href</a></iframe><p>Now, we can use the above selector in our blog view and edit components:</p><pre>constructor(private store: Store&lt;State&gt;) {<br> this.selectedBlog$ = this.store.select(getSelectedBlog);<br>}</pre><p>By being able to import one less thing into our components, we can keep things clean and separated.</p><h3>Resolvers</h3><p>In the above example, our component only gets the observable of the selected blog, which requires us to still subscribe, or use the async pipe, to have access to the actual blog object. This isn’t a big deal, but it would be nice to have access to the selected blog object right away.</p><p>We can do this by making use of resolvers, which allow us to pass async data through the activated route object. The Router waits for the data to be retrieved/resolved before the component is rendered.</p><p>A resolver is a simple class that contains a public resolve method that returns an observable or promise. Something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09c66075c2b5fcda884f261c06e52988/href">https://medium.com/media/09c66075c2b5fcda884f261c06e52988/href</a></iframe><p>We can then add it to our route config:</p><pre>{<br> path: ‘blogs/:blogId’,<br> component: BlogDetailComponent,<br> resolve: { blog: BlogResolver }<br>}</pre><p>Now, we can access the selected blog as blog through the <a href="https://angular.io/api/router/ActivatedRoute#data">activated Route data</a> object.</p><p>The resolve method waits for the observable to complete to load the route, which requires us to use the take(1) operator.</p><p>There’s one downside to this: in order to refresh the selected blog object we would need to reload the route. So any changes outside of the component won’t be reflected.</p><h3>Guards</h3><p>One problem with NgRx is, “how do I know if my data has been loaded before rendering the actual component?”</p><p><strong>The answer: Guards.</strong></p><p>Guards essentially act the same as resolvers do, but return a boolean instead. If it’s true, the route continues to its destination, and if it’s false, it cancels the route transition.</p><p>Just like the resolver, we can make use of the getSelectedBlog selector and map it to a boolean, depending on whether it exists or not. This works fine if the blog has already been loaded but what if it hasn’t yet? Or what happens if the get request failed for some reason?</p><p>A common suggestion is to add the loading and loaded attributes to the state to track if the data has already been loaded.</p><p>We can make use of this data in our guard, this time using thecanActivate function instead of the resolve function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be9c8def09a48b765dfdf7c2250a4000/href">https://medium.com/media/be9c8def09a48b765dfdf7c2250a4000/href</a></iframe><p>This might look complicated initially but will become more understandable when we walk through each step:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/0*xKD9fxx-8X14OxaA" /></figure><p>This is one great way to handle all cases to make sure the data is loaded before we load the route and render the component.</p><h3>Effects</h3><p>NgRx effects are a great way to make your asynchronous data requests and update it accordingly. This is usually done by triggering a Load or Update action that initiates the effect. Once the effect is done it triggers a Load/Update Success action. But as the name implies, this shouldn’t be the only case for using effects.</p><p>Effects can be triggered by any action and can trigger any number of actions as well. In this case, we don’t need to worry about dealing with callbacks in our components and instead just let the effects do the work for us.</p><p>Suppose we want to show a notification after the blog post has been successfully saved. Given that we made the notifications part of the NgRx store, we don’t need to touch the component but have a simple way of implementing this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aea046f073f5518a91af61e5eab9d19a/href">https://medium.com/media/aea046f073f5518a91af61e5eab9d19a/href</a></iframe><p>Returning an array, now with an update success and a notification open action. The notification open action will in turn trigger an effect that will show the popup.</p><h3>Meta-Reducers</h3><blockquote><a href="https://github.com/NgRx/platform/blob/master/docs/store/api.md#meta-reducers">Developers can think of meta-reducers as hooks into the action-reducer pipeline. Meta-reducers allow developers to pre-process actions before normal reducers are invoked.</a></blockquote><p>These meta-reducers can be set globally, and get triggered on each action. This would be very useful for logging or storing the actions in case we want to backtrack.</p><p>Below is a small example of using a Meta Reducer to log every action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce1110fa6b1891620c0512d75dc5a82c/href">https://medium.com/media/ce1110fa6b1891620c0512d75dc5a82c/href</a></iframe><p>And can be registered as a provider:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6954aea8a6181ab9006bf406c68c1814/href">https://medium.com/media/6954aea8a6181ab9006bf406c68c1814/href</a></iframe><p>Another great example of using a meta-reducer is the timeline functionality in the <a href="https://github.com/ngrx/platform/blob/master/docs/store-devtools/README.md">NgRx Store Devtools</a> that is used to keep track of all the actions.</p><h3>In Conclusion</h3><p>NgRx can certainly seem like too much for your Angular app when it comes to managing its state. The above examples show several other ways to make good use of all elements NgRx offers, all while enforcing cleaner, more reusable code.</p><p>I promise — this makes the extra bit of set up totally worth it!</p><p><em>Follow our knowledge and idea sharing culture with Tech Share — a Well Red series brought to you by REDspace’s experts — </em><a href="https://www.redspace.com/?utm_source=Medium&amp;utm_medium=Boiler%20Plate&amp;utm_campaign=tech_share_2"><em>redspace.com</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96a971380463" width="1" height="1" alt=""><hr><p><a href="https://medium.com/well-red/how-to-get-the-most-out-of-ngrx-96a971380463">How to Get the Most Out of NgRx</a> was originally published in <a href="https://medium.com/well-red">Well Red</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>