<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tomasz Kula</title>
    <description>The latest articles on DEV Community by Tomasz Kula (@realtomaszkula).</description>
    <link>https://dev.to/realtomaszkula</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F52640%2F92cd8364-d7b0-4795-a8d0-6cf9f23e734b.jpg</url>
      <title>DEV Community: Tomasz Kula</title>
      <link>https://dev.to/realtomaszkula</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/realtomaszkula"/>
    <language>en</language>
    <item>
      <title>The Most Misunderstood Part of Styling Components 🚀</title>
      <dc:creator>Tomasz Kula</dc:creator>
      <pubDate>Tue, 16 Feb 2021 13:17:33 +0000</pubDate>
      <link>https://dev.to/realtomaszkula/the-most-misunderstood-part-of-styling-angular-components-1p99</link>
      <guid>https://dev.to/realtomaszkula/the-most-misunderstood-part-of-styling-angular-components-1p99</guid>
      <description>&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;Imagine you are tasked with rendering a list of articles. Seems easy enough, you've done this a lot. You quickly come up with this solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"articles"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;*ngFor=&lt;/span&gt;&lt;span class="s"&gt;"let article of articles"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     {{ article.tittle }}
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.articles&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All is well in the world, the list is displayed correctly, and you move on with your life 💪&lt;/p&gt;

&lt;h1&gt;
  
  
  The Next Day
&lt;/h1&gt;

&lt;p&gt;Next day, there is a new requirement. On a different page of the application you have to render the same articles, but now in a grid layout 🧱&lt;/p&gt;

&lt;p&gt;We want to reuse the existing component, because the only difference is the layout of the list. &lt;/p&gt;

&lt;p&gt;You might think of the following solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create &lt;code&gt;@Input&lt;/code&gt; which will drive the behaviour of dynamically styling the component to render either a list or a grid layout&lt;/li&gt;
&lt;li&gt;create a &lt;code&gt;&amp;lt;articles-grid&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;articles-list&lt;/code&gt;&amp;gt; components which will reuse the article card.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both of this solutions will work, but they are needlessly complicated 😱 &lt;br&gt;
We either have to expand the component API to accommodate different layouts or create layout components that differ by just a couple lines of CSS.&lt;/p&gt;
&lt;h1&gt;
  
  
  The solution
&lt;/h1&gt;

&lt;p&gt;What about a different approach? If we get rid of the wrapper &lt;code&gt;.articles&lt;/code&gt; div and style the &lt;code&gt;:host&lt;/code&gt; element directly, we will be able to override this layout styling as needed in the parent component.&lt;/p&gt;

&lt;p&gt;Let's see it in action.&lt;/p&gt;

&lt;p&gt;First, we remove the not needed wrapper div:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;*ngFor=&lt;/span&gt;&lt;span class="s"&gt;"let article of articles"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"article"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   {{ article.tittle }}
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we update the CSS to target the &lt;code&gt;:host&lt;/code&gt; element of the component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nd"&gt;:host&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it! With this simple change, we open up lost of possibilities for different layouts, which we can achieve with just CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's because the &lt;code&gt;:host&lt;/code&gt; selector is the only part of the component which can be styled in the parent component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now this will continue to render the default flex layout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;app-articles&amp;gt;&amp;lt;/app-articles&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this will override the flex layout with grid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;app-articles&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"grid"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/app-articles&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.grid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;fr&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;fr&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;fr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the future if there is a new requirement to render the article list in a different layout, it's as easy as creating a new CSS class and styling it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;app-articles&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"two-column-grid"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/app-articles&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;app-articles&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"five-column-grid"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/app-articles&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;app-articles&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"responsive-grid"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/app-articles&amp;gt;&lt;/span&gt;
...etc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The recommendation
&lt;/h1&gt;

&lt;p&gt;❌ Avoid using top level container elements just to style them&lt;br&gt;
✅ Style &lt;code&gt;:host&lt;/code&gt; component when possible to make the component more reusable&lt;/p&gt;

&lt;p&gt;Hope you're having a great one, and I'll see you for more web dev posts in the future 🥳&lt;/p&gt;

&lt;h1&gt;
  
  
  In Case You Missed it
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/realtomaszkula/the-most-important-thing-to-understand-about-component-composition-3ia4"&gt;The Most Important Thing to Understand About Component Composition 🚀
&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/realtomaszkula/how-distinct-is-distinctuntilchanged-3pf9"&gt;How Distinct Is DistinctUntilChanged?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/realtomaszkula/streaming-the-angular-output-2369"&gt;Streaming the Angular Output&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>angular</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>The Most Important Thing to Understand About Component Composition 🚀</title>
      <dc:creator>Tomasz Kula</dc:creator>
      <pubDate>Fri, 12 Feb 2021 10:42:58 +0000</pubDate>
      <link>https://dev.to/realtomaszkula/the-most-important-thing-to-understand-about-component-composition-3ia4</link>
      <guid>https://dev.to/realtomaszkula/the-most-important-thing-to-understand-about-component-composition-3ia4</guid>
      <description>&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;Say you are working on an e-commerce application and are tasked with creating a product page that renders a call to action button to buy awesome Nike shoes.&lt;/p&gt;

&lt;p&gt;Right now, the app has a following component tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; product-page
&amp;gt;&amp;gt; card
&amp;gt;&amp;gt;&amp;gt; card-content
&amp;gt;&amp;gt;&amp;gt;&amp;gt; cta-button
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You want to render "Buy Air Max" as a label of the call to action button. &lt;/p&gt;

&lt;p&gt;The issue is the button component is deeply nested in the component tree, and the product model information is available in the top most component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; product-page &amp;lt;-- Here we know the product name
&amp;gt;&amp;gt; card
&amp;gt;&amp;gt;&amp;gt; card-content
&amp;gt;&amp;gt;&amp;gt;&amp;gt; cta-button  &amp;lt;-- Here we want to render the product name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The Struggle
&lt;/h1&gt;

&lt;p&gt;You might jump in and create the &lt;code&gt;@Input()&lt;/code&gt; in each component and pass it down to the button component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- product-page.component --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;card&lt;/span&gt; &lt;span class="na"&gt;[productName]=&lt;/span&gt;&lt;span class="s"&gt;"productName"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/card&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- card.component --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;card-content&lt;/span&gt; &lt;span class="na"&gt;[productName]=&lt;/span&gt;&lt;span class="s"&gt;"productName"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/card-content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- card-content.component --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;cta-button&lt;/span&gt; &lt;span class="na"&gt;[productName]=&lt;/span&gt;&lt;span class="s"&gt;"productName"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/cta-button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- cta-button.component --&amp;gt;&lt;/span&gt;
Buy {{ productName }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works fine, but there are couple of issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are adding &lt;code&gt;@Inputs()&lt;/code&gt; to components which don't use them, only passing them down to the child components.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; product-page 
&amp;gt;&amp;gt; card &amp;lt;-- I don't care about the product name
&amp;gt;&amp;gt;&amp;gt; card-content &amp;lt;-- I don't care about the product name
&amp;gt;&amp;gt;&amp;gt;&amp;gt; cta-button  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;It makes adding additional &lt;code&gt;@Inputs()&lt;/code&gt; painful. If button needs more data from the product page, you have to pass it through two other components. This process is sometimes referred to as prop drilling.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; product-page &amp;lt;- We know the product price
&amp;gt;&amp;gt; card &amp;lt;-- I have to add @Input() for price
&amp;gt;&amp;gt;&amp;gt; card-content &amp;lt;-- I have to add @Input() for price
&amp;gt;&amp;gt;&amp;gt;&amp;gt; cta-button &amp;lt;-- I have to add @Input() for price
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;It makes unit testing more difficult as you have to test passing the &lt;code&gt;@Inputs()&lt;/code&gt; in each component.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The Solution
&lt;/h1&gt;

&lt;p&gt;You can solve this problem using a different approach. How about we use content projection instead of drilling props with &lt;code&gt;@Inputs()&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Compare the previous solution, with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- product-page.component --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;card&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;card-content&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;cta-button&amp;gt;&lt;/span&gt;Buy {{ productName }}&lt;span class="nt"&gt;&amp;lt;/cta-button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/card-content&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/card&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- card.component --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- card-content.component --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- cta-button.component --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach has the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We no longer add &lt;code&gt;@Inputs()&lt;/code&gt; to components that don't need them so we avoid prop drilling.&lt;/li&gt;
&lt;li&gt;Components become more extensible. You can pass as much information to the button component as you'd like, without touching the card components.&lt;/li&gt;
&lt;li&gt;Because of the previous points, unit testing becomes much easier.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The Benefits
&lt;/h1&gt;

&lt;p&gt;Let's see how we might benefit from this approach.&lt;/p&gt;

&lt;p&gt;Say you are now tasked with extending the call to action button with a price tag - "Buy Air Max at $199".&lt;/p&gt;

&lt;p&gt;With content projection approach, we only need to make a small change in the product page component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- product-page.component --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;card&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;card-content&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;cta-button&amp;gt;&lt;/span&gt;Buy {{ productName }} at {{ productPrice }}&lt;span class="nt"&gt;&amp;lt;/cta-button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/card-content&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/card&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! See how easy it is? No props drilling, no test changes for the child components, no problem :)&lt;/p&gt;

&lt;p&gt;Hope you're having a great one, and I'll see you for more web dev posts in the future 🥳&lt;/p&gt;

&lt;h1&gt;
  
  
  In Case You Missed it
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/realtomaszkula/how-distinct-is-distinctuntilchanged-3pf9"&gt;How Distinct Is DistinctUntilChanged?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/realtomaszkula/streaming-the-angular-output-2369"&gt;Streaming the Angular Output&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>angular</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Streaming the Angular @Output()</title>
      <dc:creator>Tomasz Kula</dc:creator>
      <pubDate>Mon, 08 Feb 2021 08:23:46 +0000</pubDate>
      <link>https://dev.to/realtomaszkula/streaming-the-angular-output-2369</link>
      <guid>https://dev.to/realtomaszkula/streaming-the-angular-output-2369</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IzhGBXmb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/toq3dsuxulglb079r87z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IzhGBXmb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/toq3dsuxulglb079r87z.png" alt="main"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We all know and love the &lt;code&gt;@Output()&lt;/code&gt; decorator. If you are working with Angular I'm sure you've done this before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;change&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ChangeEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the lesser known fact is, that the property decorated by the &lt;code&gt;@Output&lt;/code&gt; does not have to be an &lt;code&gt;EventEmitter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In fact, it can be an RxJS &lt;code&gt;Observable&lt;/code&gt;. It opens up a lot of possibilities, because we can wield the full power of RxJS operators 😱&lt;/p&gt;

&lt;p&gt;For example, you can have an &lt;code&gt;Output()&lt;/code&gt; decorating your reactive form control's &lt;code&gt;valueChanges&lt;/code&gt; stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;control&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;FormControl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;valueChages$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueChanges&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="nx"&gt;distinctUntilChanged&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can see I'm limiting the emitted values to only the distinct ones, but other operators such as &lt;code&gt;debounceTime&lt;/code&gt; could be used as well.&lt;/p&gt;

&lt;p&gt;Hope you're having a great one, and I'll see you for more &lt;strong&gt;60 Seconds of Angular&lt;/strong&gt; posts in the future 🥳&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackblitz.com/edit/60-seconds-of-angular-output?file=src/app/app.component.html"&gt;Live demo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>rxjs</category>
    </item>
    <item>
      <title>How distinct is distinctUntilChanged?</title>
      <dc:creator>Tomasz Kula</dc:creator>
      <pubDate>Mon, 08 Feb 2021 08:23:15 +0000</pubDate>
      <link>https://dev.to/realtomaszkula/how-distinct-is-distinctuntilchanged-3pf9</link>
      <guid>https://dev.to/realtomaszkula/how-distinct-is-distinctuntilchanged-3pf9</guid>
      <description>&lt;p&gt;If you have been working with RxJS, you might have heard about &lt;code&gt;distinctUntilChanged()&lt;/code&gt; operator 🔥&lt;/p&gt;

&lt;p&gt;Let's have a quick peek into the official &lt;a href="https://rxjs-dev.firebaseapp.com/api/operators/distinctUntilChanged"&gt;docs&lt;/a&gt; to see how it works: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from the previous item.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So in short, if the source stream emits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a-a-b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get just&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a---b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a catch, though. By default, the operator will compare values using the &lt;code&gt;===&lt;/code&gt; operator. &lt;/p&gt;

&lt;p&gt;This will work fine for primitive values like strings or numbers which are compared by value, but will not work for stuff like arrays or objects which are compared by reference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;     &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;   &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;   &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So any time you are working with objects streams, you should strongly consider providing your own compare function to the &lt;code&gt;distinctUntilChanged()&lt;/code&gt; operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// ❌ does not work, because values are compared with ===&lt;/span&gt;
&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;distinctUntilChanged&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// logs { id: 1 } { id: 1 } { id: 2 }&lt;/span&gt;

&lt;span class="c1"&gt;// 🔥 it works, because we provide a custom compare function&lt;/span&gt;
&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;distinctUntilChanged&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// logs { id: 1 } { id: 2 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://stackblitz.com/edit/60-seconds-distinct-until-changed?file=index.ts"&gt;Live demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you're having a great one, and I'll see you for more 60 Seconds posts in the future 🥳&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>rxjs</category>
    </item>
  </channel>
</rss>
