<?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 Piyush Jain on Medium]]></title>
        <description><![CDATA[Stories by Piyush Jain on Medium]]></description>
        <link>https://medium.com/@jnpiyush?source=rss-d63f3f458a46------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*rmmTG3cN7_hnDB9k</url>
            <title>Stories by Piyush Jain on Medium</title>
            <link>https://medium.com/@jnpiyush?source=rss-d63f3f458a46------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 13:35:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jnpiyush/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[Compound component in React]]></title>
            <link>https://jnpiyush.medium.com/compound-component-in-react-31a89fe3751a?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/31a89fe3751a</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ui]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Fri, 11 Oct 2024 10:27:34 GMT</pubDate>
            <atom:updated>2024-10-11T10:27:34.165Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="React Compound Components" src="https://cdn-images-1.medium.com/max/825/1*y7u1sw9jSeZpIh0jUKPDjA.png" /><figcaption>Source — <a href="https://miro.medium.com/v2/0*bTWTbzMf10xI5XlD.png">https://miro.medium.com/v2/0*bTWTbzMf10xI5XlD.png</a></figcaption></figure><h4>What is Compound Component</h4><p>Compound component is a group of child components which helps to create complex components as highly reusable.</p><p>For example if we have a requirement to create a reusable card component which can be use on multiple places and it can have a header, content and action buttons.<br>There can a scenario and on some place we need all those 3 sections but on some place we need only few of them not all.</p><p>Lets say in scenario 2 we don’t want to show description and action buttons and in scenario 3 we don’t want to show action buttons.</p><p>Our card component’s template looks like this.</p><pre>export default function Card() {<br>  return (<br>    &lt;div className=&quot;card&quot;&gt;<br>      &lt;div className=&quot;card-header&quot;&gt;<br>        &lt;h3&gt;Labrador&lt;/h3&gt;<br>        &lt;div&gt;Dog Breed&lt;/div&gt;<br>      &lt;/div&gt;<br><br>      &lt;img<br>        src=&quot;https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTqRNGjpgj_trfUKqFwEvpcq1ttRPzgmQXLNQJgKUIMS0lqDKuY&quot;<br>        alt=&quot;Photo of a Shiba Inu&quot;<br>      /&gt;<br><br>      &lt;div className=&quot;card-contenr&quot;&gt;<br>        &lt;p&gt;<br>          The Labrador Retriever or simply Labrador is a British breed of<br>          retriever gun dog. It was developed in the United Kingdom from St.<br>          John&#39;s water dogs imported from the colony of Newfoundland, and was<br>          named after the Labrador region of that colony.<br>        &lt;/p&gt;<br>      &lt;/div&gt;<br><br>      &lt;div className=&quot;card-actions&quot;&gt;<br>        &lt;button&gt;LIKE&lt;/button&gt;<br>        &lt;button&gt;SHARE&lt;/button&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*zRCz7PVU-4YKHOJl0uGTtw.png" /></figure><p>To make it compound component we can can change it like below code.</p><pre>import &quot;./Card.css&quot;;<br><br>export default function Card({ children }) {<br>  return &lt;div className=&quot;card&quot;&gt;{children}&lt;/div&gt;;<br>}<br><br>export function CardHeader({ children }) {<br>  return &lt;div className=&quot;card-header&quot;&gt;{children}&lt;/div&gt;;<br>}<br><br>export function CartTitle() {<br>  return &lt;h3&gt;Labrador&lt;/h3&gt;;<br>}<br><br>export function CartSubTitle() {<br>  return &lt;div&gt;Dog Breed&lt;/div&gt;;<br>}<br><br>export function CardImage() {<br>  return (<br>    &lt;img<br>      src=&quot;https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTqRNGjpgj_trfUKqFwEvpcq1ttRPzgmQXLNQJgKUIMS0lqDKuY&quot;<br>      alt=&quot;Photo of a Shiba Inu&quot;<br>    /&gt;<br>  );<br>}<br><br>export function CardContent() {<br>  return (<br>    &lt;div className=&quot;card-contenr&quot;&gt;<br>      &lt;p&gt;<br>        The Labrador Retriever or simply Labrador is a British breed of<br>        retriever gun dog. It was developed in the United Kingdom from St.<br>        John&#39;s water dogs imported from the colony of Newfoundland, and was<br>        named after the Labrador region of that colony.<br>      &lt;/p&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export function CardActions({ children }) {<br>  return &lt;div className=&quot;card-actions&quot;&gt;{children}&lt;/div&gt;;<br>}<br><br>export function CardLikeButton() {<br>  return &lt;button&gt;LIKE&lt;/button&gt;;<br>}<br><br>export function CardShareButton() {<br>  return &lt;button&gt;SHARE&lt;/button&gt;;<br>}<br></pre><p>And we can use it in parent component like below code.</p><pre>function App() {<br>  return (<br>    &lt;&gt;<br>      &lt;Card&gt;<br>        &lt;CardHeader&gt;<br>          &lt;CartTitle /&gt;<br>          &lt;CartSubTitle /&gt;<br>        &lt;/CardHeader&gt;<br><br>        &lt;CardImage /&gt;<br><br>        &lt;CardContent /&gt;<br><br>        &lt;CardActions&gt;<br>          &lt;CardLikeButton /&gt;<br>          &lt;CardShareButton /&gt;<br>        &lt;/CardActions&gt;<br>      &lt;/Card&gt;<br>    &lt;/&gt;<br>  );<br>}<br><br>export default App;</pre><p>If you will run this code it will still look same.</p><p>Now for our scenario 2 where we don’t want to show description / content we can use Card component like below.</p><pre>function App() {<br>  return (<br>    &lt;&gt;<br>      &lt;Card&gt;<br>        &lt;CardHeader&gt;<br>          &lt;CartTitle /&gt;<br>          &lt;CartSubTitle /&gt;<br>        &lt;/CardHeader&gt;<br><br>        &lt;CardImage /&gt;<br><br>        &lt;CardActions&gt;<br>          &lt;CardLikeButton /&gt;<br>          &lt;CardShareButton /&gt;<br>        &lt;/CardActions&gt;<br>      &lt;/Card&gt;<br>    &lt;/&gt;<br>  );<br>}<br><br>export default App;</pre><p>And it will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*NJj1IcW8mT2Sz1Rp79yehQ.png" /></figure><p>For our scenario 3 where we don’t want to show action button we can use Card component like below.</p><pre>function App() {<br>  return (<br>    &lt;&gt;<br>      &lt;Card&gt;<br>        &lt;CardHeader&gt;<br>          &lt;CartTitle /&gt;<br>          &lt;CartSubTitle /&gt;<br>        &lt;/CardHeader&gt;<br><br>        &lt;CardImage /&gt;<br><br>        &lt;CardContent /&gt;<br>      &lt;/Card&gt;<br>    &lt;/&gt;<br>  );<br>}<br><br>export default App;</pre><p>And it will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*doyo-bhy5jFtFABuWdOwjg.png" /></figure><p>So, now we don’t have to do any changes in Card component for any scenario.</p><p>This can be use to create product card where we need to show rating and other thing related to product based on requirements.</p><p>I hope you liked this article.</p><p>You can play with this code here — <a href="https://codesandbox.io/p/github/piyush303/React-Compound-Component">https://codesandbox.io/p/github/piyush303/React-Compound-Component</a><br>github — <a href="https://github.com/piyush303/React-Compound-Component">https://github.com/piyush303/React-Compound-Component</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31a89fe3751a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NgRx Signal Store — How to write Unit Test]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://jnpiyush.medium.com/ngrx-signal-store-how-to-write-unit-test-3399fb63f344?source=rss-d63f3f458a46------2"><img src="https://cdn-images-1.medium.com/max/820/1*UBLFcmUBuG_vryGw5WKHTw.jpeg" width="820"></a></p><p class="medium-feed-snippet">In this article we will learn how to write unit test for NgRx Signal Store. We will be using SIFERS approach to write unit test.</p><p class="medium-feed-link"><a href="https://jnpiyush.medium.com/ngrx-signal-store-how-to-write-unit-test-3399fb63f344?source=rss-d63f3f458a46------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://jnpiyush.medium.com/ngrx-signal-store-how-to-write-unit-test-3399fb63f344?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/3399fb63f344</guid>
            <category><![CDATA[unittest]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[signal]]></category>
            <category><![CDATA[jest]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Fri, 19 Jul 2024 08:05:50 GMT</pubDate>
            <atom:updated>2024-07-19T11:51:51.417Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Angular Unit Test: Mock Service and Observables — Part 1]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://jnpiyush.medium.com/angular-unit-test-mock-service-and-observables-part-1-923aa45be42f?source=rss-d63f3f458a46------2"><img src="https://cdn-images-1.medium.com/max/900/1*E0_TIrtocoue6saFFyOtdw.png" width="900"></a></p><p class="medium-feed-snippet">In this article we will learn how to mock Services and Observable and write unit test.</p><p class="medium-feed-link"><a href="https://jnpiyush.medium.com/angular-unit-test-mock-service-and-observables-part-1-923aa45be42f?source=rss-d63f3f458a46------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://jnpiyush.medium.com/angular-unit-test-mock-service-and-observables-part-1-923aa45be42f?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/923aa45be42f</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[integration]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[jest]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Fri, 09 Sep 2022 17:33:56 GMT</pubDate>
            <atom:updated>2022-09-09T17:33:56.427Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How to create nested dynamic routes using React Router v6]]></title>
            <link>https://jnpiyush.medium.com/how-to-create-nested-dynamic-routes-using-react-router-v6-96edc4daa061?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/96edc4daa061</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react-router]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Wed, 20 Apr 2022 16:28:46 GMT</pubDate>
            <atom:updated>2022-04-20T16:28:46.690Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E8oLE72j5c4FJZDZaoWm0Q.jpeg" /></figure><p>In this tutorial we will learn how to create nested routed using React Router v6.</p><p>This is how our final result look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*ek62nb9KXVkjSeUsJojATQ.gif" /><figcaption>nested dynamic routes using React Router v6</figcaption></figure><p>First of all we will create 2 components one for Home and another one for User.</p><p>Now we will create 2 new router and will add navigation for them in App.Js. We will create 2 more components apart from mentioned above one for Layout and another one for NotFound.</p><pre>import { Link, Routes, Route, BrowserRouter as Router } from &#39;react-router-dom&#39;;<br>import Home from &#39;./components/Home&#39;;<br>import NotFound from &#39;./components/NotFound&#39;;<br>import User from &#39;./components/User&#39;;<br>import Layout from &#39;./components/Layout&#39;;</pre><pre>import &#39;./App.css&#39;;</pre><pre>const App = () =&gt; {</pre><pre>return (<br>    &lt;Router&gt;<br>      &lt;h1&gt;React Router&lt;/h1&gt;</pre><pre>      &lt;nav&gt;<br>        &lt;Link to=&quot;/home&quot;&gt;Home&lt;/Link&gt;<br>        &lt;Link to=&quot;/user&quot;&gt;User&lt;/Link&gt;<br>      &lt;/nav&gt;</pre><pre>      &lt;Routes&gt;<br>        &lt;Route element={&lt;Layout /&gt;}&gt;<br>          &lt;Route index element={&lt;Home /&gt;} /&gt;<br>          &lt;Route path=&quot;home&quot; element={&lt;Home /&gt;} /&gt;<br>          &lt;Route path=&quot;user&quot; element={&lt;User /&gt;} /&gt;<br>          &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;<br>        &lt;/Route&gt;<br>      &lt;/Routes&gt;<br>    &lt;/Router&gt;<br>  );<br>}</pre><p>In this functional component we have matching Link and Route component from React Router for /home and /user routes. Then we have index route loaded with Home Component and a NoFound / WildCard route loaded with NotFound component.</p><p>Both works as fallback components.</p><h4>Nested Routes</h4><p>In User Component we will add nested routing. So, we will add a Link components which will navigate user to their Profile and Transactions.</p><pre>import React from &#39;react&#39;;<br>import { Link } from &#39;react-router-dom&#39;;</pre><pre>const User = () =&gt; {<br> return (<br>  &lt;&gt;<br>   &lt;h1&gt;User&lt;/h1&gt;</pre><pre>   &lt;nav&gt;<br>    &lt;Link to=&quot;profile&quot;&gt;Profile&lt;/Link&gt;<br>    &lt;Link to=&quot;transactions&quot;&gt;Transactions&lt;/Link&gt;<br>   &lt;/nav&gt;<br>  &lt;/&gt;<br> )<br>}</pre><pre>export default User;</pre><p>If we click one of these link we would be redirected to NotFound route. This means we haven’t mapped these routes (/user/profile, /user/transactions) to actual route component yet. Therefore we will add these nested in routes to our /user routes.</p><pre>import { Link, Routes, Route, BrowserRouter as Router } from &#39;react-router-dom&#39;;<br>import Home from &#39;./components/Home&#39;;<br>import NotFound from &#39;./components/NotFound&#39;;<br>import User from &#39;./components/User&#39;;<br>import Layout from &#39;./components/Layout&#39;;<br>import Profile from &#39;./components/Profile&#39;;<br>import Transactions from &#39;./components/Transactions&#39;;</pre><pre>const App = () =&gt; {</pre><pre>return (<br>    &lt;Router&gt;<br>      &lt;h1&gt;React Router&lt;/h1&gt;</pre><pre>      &lt;nav&gt;<br>        &lt;Link to=&quot;/home&quot;&gt;Home&lt;/Link&gt;<br>        &lt;Link to=&quot;/user&quot;&gt;User&lt;/Link&gt;<br>      &lt;/nav&gt;</pre><pre>      &lt;Routes&gt;<br>        &lt;Route element={&lt;Layout /&gt;}&gt;<br>          &lt;Route index element={&lt;Home /&gt;} /&gt;<br>          &lt;Route path=&quot;home&quot; element={&lt;Home /&gt;} /&gt;<br>          &lt;Route path=&quot;user&quot; element={&lt;User /&gt;}&gt;<br>            &lt;Route path=&quot;profile&quot; element={&lt;Profile /&gt;} /&gt;<br>            &lt;Route path=&quot;transactions&quot; element={&lt;Transactions /&gt;} /&gt;<br>          &lt;/Route&gt;<br>          &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;<br>        &lt;/Route&gt;<br>      &lt;/Routes&gt;<br>    &lt;/Router&gt;<br>  );<br>}</pre><pre>export default App;</pre><p>The Route components map to the Link components in a one to one relationship now. However, there can be more than one Link component linking to the same route, so it’s actually a one to many relationship.</p><p>If we test this now in browser we will see that only User Component shows up in the browser not its nested component which are Profile and Transactions which clicking their respective links.</p><p>To make it work we will have to add Outlet component from React Router.</p><pre>import React from &#39;react&#39;;<br>import { Link, Outlet } from &#39;react-router-dom&#39;;</pre><pre>const User = () =&gt; {<br> return (<br>  &lt;&gt;<br>   &lt;h2&gt;User&lt;/h2&gt;</pre><pre>   &lt;nav&gt;<br>    &lt;Link to=&quot;profile&quot;&gt;Profile&lt;/Link&gt;<br>    &lt;Link to=&quot;transactions&quot;&gt;Transactions&lt;/Link&gt;<br>   &lt;/nav&gt;</pre><pre>   &lt;Outlet /&gt;<br>  &lt;/&gt;<br> )<br>}</pre><pre>export default User;</pre><p>The Outlet component renders the matching child route with its respective component (here either Profile or Transactions component) from the parent Routes’ component collection of Route components.</p><p>If there is not /profile or /transactions matching route (e.g. /user/profile), we will see only User component showing up. So, to avoid this we can add index and NotFound Route. Now the default route will be Profile.</p><pre>const App = () =&gt; {</pre><pre>return (<br>    &lt;Router&gt;<br>      &lt;h1&gt;React Router&lt;/h1&gt;</pre><pre>      &lt;nav&gt;<br>        &lt;Link to=&quot;/home&quot;&gt;Home&lt;/Link&gt;<br>        &lt;Link to=&quot;/user&quot;&gt;User&lt;/Link&gt;<br>      &lt;/nav&gt;</pre><pre>      &lt;Routes&gt;<br>        &lt;Route element={&lt;Layout /&gt;}&gt;<br>          &lt;Route index element={&lt;Home /&gt;} /&gt;<br>          &lt;Route path=&quot;home&quot; element={&lt;Home /&gt;} /&gt;<br>          &lt;Route path=&quot;user&quot; element={&lt;User /&gt;}&gt;<br>            <strong>&lt;Route index element={&lt;Profile /&gt;} /&gt;</strong><br>            &lt;Route path=&quot;profile&quot; element={&lt;Profile /&gt;} /&gt;<br>            &lt;Route path=&quot;transactions&quot; element={&lt;Transactions /&gt;} /&gt;<br>            &lt;<strong>Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;</strong><br>          &lt;/Route&gt;<br>          &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;<br>        &lt;/Route&gt;<br>      &lt;/Routes&gt;<br>    &lt;/Router&gt;<br>  );<br>}</pre><p>That’s it. While the User component always renders the navigation, its content (Outlet) gets replaced by the matching nested route (either Profile or Transactions component based on /user/profile or /user/transactions route). If none of these routes are matched when visiting the /user route, the application will show either the Profile component (if route matches exactly /user) or the NoMatch component (if route does not match, e.g. /user/setting) showing up.</p><h4>Dynamic Nested Route</h4><p>In this section we will render dynamic nested route based on identifiers /user/transactions/1 (Transactions details with transaction id 1).</p><p>Let’s start by initializing a list of transactions in our App Component. This is just a simple data but can be fetch from remote API. We will pass this data in Transactions Component as props.</p><pre>const App = () =&gt; {<br> <strong> const transactions = [<br>    { id: &#39;1&#39;, details: &#39;Transaction 1&#39; },<br>    { id: &#39;2&#39;, details: &#39;Transactions 2&#39; },<br>  ];</strong></pre><pre>return (<br>    &lt;Router&gt;<br>      &lt;h1&gt;React Router&lt;/h1&gt;</pre><pre>      &lt;nav&gt;<br>        &lt;Link to=&quot;/home&quot;&gt;Home&lt;/Link&gt;<br>        &lt;Link to=&quot;/user&quot;&gt;User&lt;/Link&gt;<br>      &lt;/nav&gt;</pre><pre>      &lt;Routes&gt;<br>        &lt;Route element={&lt;Layout /&gt;}&gt;<br>          &lt;Route index element={&lt;Home /&gt;} /&gt;<br>          &lt;Route path=&quot;home&quot; element={&lt;Home /&gt;} /&gt;<br>          &lt;Route path=&quot;user&quot; element={&lt;User /&gt;}&gt;<br>            &lt;Route index element={&lt;Profile /&gt;} /&gt;<br>            &lt;Route path=&quot;profile&quot; element={&lt;Profile /&gt;} /&gt;<br>            <strong>&lt;Route path=&quot;transactions&quot; element={&lt;Transactions transactions={transactions} /&gt;} /&gt;</strong><br>            &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;<br>          &lt;/Route&gt;<br>          &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;<br>        &lt;/Route&gt;<br>      &lt;/Routes&gt;<br>    &lt;/Router&gt;<br>  );<br>}</pre><p>Transactions component becomes list component here, where we will be showing list of transaction for the user using Link Component. The relative path in the Link component hints to a respective nested (here: /${transaction.id} nested in /user/transactions) yet dynamic (here: /${transaction.id}) route.</p><pre><strong>const Transactions = ({ transactions }) =&gt; {</strong><br> return (<br>  &lt;&gt;<br>   &lt;h2&gt;Transactions&lt;/h2&gt;</pre><pre><strong>      &lt;ul&gt;<br>        {transactions.map((transaction) =&gt; (<br>          &lt;li key={transaction.id}&gt;<br>            &lt;Link to={transaction.id}&gt;<br>              {transaction.details}<br>            &lt;/Link&gt;<br>          &lt;/li&gt;<br>        ))}<br>      &lt;/ul&gt;</strong><br>  &lt;/&gt;<br> )<br>}</pre><p>Now we need to create a matching nested route in App Component. First it is <strong>Nested Route</strong> of /user/transactions, we can place it in respective parent route component. Secondly it is dynamic route, it uses dynamic route which is defined as :transactionId , where transactionId is dynamic.</p><p>Now we will add Outlet Component in Transactions Component so that it can render matched child route.</p><pre>const Transactions = ({ transactions }) =&gt; {<br> return (<br>  &lt;&gt;<br>   &lt;h2&gt;Transactions&lt;/h2&gt;</pre><pre>      &lt;ul&gt;<br>        {transactions.map((transaction) =&gt; (<br>          &lt;li key={transaction.id}&gt;<br>            &lt;Link to={transaction.id}&gt;<br>              {transaction.details}<br>            &lt;/Link&gt;<br>          &lt;/li&gt;<br>        ))}<br>      &lt;/ul&gt;</pre><pre>      <strong>&lt;Outlet /&gt;</strong><br>  &lt;/&gt;<br> )<br>}</pre><p>Now we will create Transaction Component, which will be render in Transaction Component via Outlet whenever there is a matched route.</p><pre>import React from &#39;react&#39;;<br>import { Link, useParams } from &#39;react-router-dom&#39;;</pre><pre>const Transaction = () =&gt; {<br> const { transactionId } = useParams();</pre><pre>return (<br>  &lt;&gt;<br>   &lt;h2&gt;Transaction Id: { transactionId }&lt;/h2&gt;<br>   &lt;Link to={-1}&gt;Back to Transactions&lt;/Link&gt;<br>  &lt;/&gt;<br> )<br>}</pre><pre>export default Transaction;</pre><p>Thats it. In Transaction Component we are getting transaction id via <a href="https://reactrouter.com/docs/en/v6/api#useparams">useParam Hook</a> which we defined as Dynamic in App Component.</p><p>According to React Router doc</p><blockquote>The useParams hook returns an object of key/value pairs of the dynamic params from the current URL that were matched by the &lt;Route path&gt;. Child routes inherit all params from their parent routes.</blockquote><p>Also we have added a <strong>Back to Transactions </strong>link in Transaction Component which will redirect back to /user/transactions route.</p><p>That it. In this article we have seen how to create nested routes and dynamic route using react router.</p><p>Nested Routes and Dynamic Routes help you to enhance your experience and maintain structured routes.</p><p>If you liked this please give a clap.</p><p>Source Code: <a href="https://github.com/piyush303/react-router-v6-nested-routes-demo">https://github.com/piyush303/react-router-v6-nested-routes-demo</a></p><p>Demo: <a href="https://codesandbox.io/s/nested-dynamic-routes-using-react-router-v6-9gs2uq">https://codesandbox.io/s/nested-dynamic-routes-using-react-router-v6-9gs2uq</a></p><p>Let us know which topic you want us to cover in next articles.</p><h4>Read Next</h4><ul><li><a href="https://jnpiyush.medium.com/angular-deep-dive-into-candeactivate-route-guard-51b9e1b046da">Angular Deep Dive into CanDeactivate Route Guard</a></li><li><a href="https://jnpiyush.medium.com/how-to-set-up-jest-in-angular-app-961ddcbab8af">How to set up Jest in Angular App</a></li><li><a href="https://jnpiyush.medium.com/how-to-use-angular-component-recursively-40591026117f">How to use Angular Component Recursively</a></li><li><a href="https://jnpiyush.medium.com/angular-deep-dive-into-providedin-scopes-be768194b541">Angular Deep dive into ProvidedIn scopes</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96edc4daa061" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Deep dive into ProvidedIn scopes]]></title>
            <link>https://jnpiyush.medium.com/angular-deep-dive-into-providedin-scopes-be768194b541?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/be768194b541</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Mon, 18 Apr 2022 10:14:17 GMT</pubDate>
            <atom:updated>2022-04-18T10:17:28.323Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mJZrKGRH2yfFXR8utBvQkQ.jpeg" /><figcaption>Tree Shaking</figcaption></figure><p>In this tutorial we will learn about using ProvidedIn.</p><p>Before Angular version 6 we used to add our services in Providers array of NgNodule.</p><p>When Angular released version 6, it introduced ProvidedIn: ‘root’.</p><p>Advantages:</p><ul><li>Services are tree-shakable</li><li>Services are singleton for the whole application, also for lazy loaded modules</li><li>No need to explicitly register the service with a NgModule</li></ul><h4>What is TreeShaking</h4><p>As per <a href="https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking">MDN</a> docs</p><blockquote><strong>Tree shaking is a term commonly used within a JavaScript context to describe the removal of dead code.</strong></blockquote><blockquote><strong>It relies on the </strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import"><strong>import</strong></a><strong> and </strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export"><strong>export</strong></a><strong> statements in ES2015 to detect if code modules are exported and imported for use between JavaScript files.</strong></blockquote><blockquote><strong>In modern JavaScript applications, we use module bundlers (e.g., </strong><a href="https://webpack.js.org/"><strong>webpack</strong></a><strong> or </strong><a href="https://github.com/rollup/rollup"><strong>Rollup</strong></a><strong>) to automatically remove dead code when bundling multiple JavaScript files into single files. This is important for preparing code that is production ready, for example with clean structures and minimal file size.</strong></blockquote><p>Angular 6 introduced ProvidedIn which we can use in <a href="https://angular.io/api/core/Injectable">@Injectable Decorator</a>.</p><p>Angular 9 introduces 2 new ways of defining scope of ProvidedIn which are any , platform .</p><p>In this tutorial we will be discussing about all possible value of ProvidedIn which are “root”, “any”, “platform”, “&lt;FeatureModule&gt;”.</p><h3>ProvidedIn root</h3><p>When you use ProvidedIn: &#39;root’option, Angular registers application-level singleton service.</p><pre>@Injectable({<br>  providedIn: &#39;root&#39;<br>})</pre><pre>export <strong>class</strong> SomeService{}</pre><p>The root option registers the service in the Root Module Injector of the Module Injector tree. This makes it available to the entire application irrespective of whether the service is lazy loaded or eagerly loaded.</p><p>If it is never used it will not be added in the final build (tree shaking).</p><h3>ProvidedIn any</h3><p>Use ProvidedIn: any when you want every lazy-loaded module to get its own instance of the service.</p><pre>@Injectable({<br>  providedIn: &#39;any&#39;<br>})</pre><pre>export <strong>class</strong> SomeService{}</pre><p>The eagerly loaded modules always share the instance provided by the Root Module Injector. Hence this will not have any effect on them.</p><h3>ProvidedIn platform</h3><p>As per the <a href="https://angular.io/api/core/Injectable">documents</a></p><p><em>A special singleton platform injector shared by all applications on the page</em>.</p><p>the platform allows us to add the service to the Providers of the Platform Injector. If you recall, the Platform Injector is the parent of the Root Module Injector in the <a href="https://angular.io/guide/hierarchical-dependency-injection#moduleinjector">Module Injector tree</a></p><pre>@Injectable({<br>  providedIn: &#39;platform&#39;<br>})</pre><pre>export <strong>class</strong> SomeService{}</pre><p>This is useful if you have multiple Angular Apps running on a single page.</p><p>This is a useful option if you are using Angular Elements, where they can share a single instance of service between them.</p><h4>ProvidedIn &lt;FeatureModule&gt;</h4><p>providedIn: FeatureModulebeing would guarantee that the service can only be used when FeatureModulebeing imported, same as configuring provider in its metadata.</p><pre>@Injectable({<br>  providedIn: &#39;FeatureModule&#39;<br>})</pre><pre>export <strong>class</strong> SomeService{}</pre><p>If you liked this please give a clap.</p><h4><strong>Read Next</strong></h4><ul><li><a href="https://jnpiyush.medium.com/how-to-set-up-jest-in-angular-app-961ddcbab8af">How to set up Jest in Angular App</a></li><li><a href="https://jnpiyush.medium.com/angular-deep-dive-into-candeactivate-route-guard-51b9e1b046da">Angular Deep Dive into CanDeactivate Route Guard</a></li><li><a href="https://jnpiyush.medium.com/how-to-build-reusable-table-component-in-angular-7a7ce79d2754">How to build Reusable Table Component in Angular</a></li><li><a href="https://jnpiyush.medium.com/how-to-create-custom-theme-in-angular-material-bd518818d2fc">How to create custom theme in Angular Material</a></li></ul><h4>References:</h4><p><a href="https://www.tektutorialshub.com/angular/providedin-root-any-platform-in-angular/">https://www.tektutorialshub.com/angular/providedin-root-any-platform-in-angular/</a></p><p><a href="https://dev.to/christiankohler/improved-dependeny-injection-with-the-new-providedin-scopes-any-and-platform-30bb">https://dev.to/christiankohler/improved-dependeny-injection-with-the-new-providedin-scopes-any-and-platform-30bb</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be768194b541" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Angular Component Recursively]]></title>
            <link>https://jnpiyush.medium.com/how-to-use-angular-component-recursively-40591026117f?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/40591026117f</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Sun, 10 Apr 2022 09:39:57 GMT</pubDate>
            <atom:updated>2022-04-10T09:39:57.970Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial we will learn how to Recursively call Angular Component.</p><p>Example: Nested Comments, Nested Menu</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TpiBPu-F31lvBZZJaaFNxA.jpeg" /><figcaption>Recursion</figcaption></figure><p>If you will go along with me final result will look like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*u_jeghrHotQt8Rg1Jco14w.png" /><figcaption>Nested Comments</figcaption></figure><p>Without wasting any time let’s start.</p><h4>Interface</h4><p>First of all we will create an interface in models/Comment.ts</p><pre>export default interface Comment {<br>  text: string;<br>  children?: Array&lt;Comment&gt;<br>}</pre><h4>Schema</h4><p>Now we will create schema in data/comment-schema.ts</p><pre>import Comment from &quot;../models/Comment&quot;;</pre><pre>export const CommentSchema: Comment[] = [<br>  {<br>    &quot;text&quot;: &quot;comment 1&quot;,<br>    &quot;children&quot;: [<br>      {<br>        &quot;text&quot;: &quot;comment 1.1&quot;,<br>        &quot;childeren&quot;: [<br>          {<br>            &quot;text&quot;: &quot;comment 1.1.1&quot;<br>          }<br>        ]<br>      },<br>      {<br>        &quot;text&quot;: &quot;comment 1.2&quot;,<br>        &quot;childeren&quot;: [<br>          {<br>            &quot;text&quot;: &quot;comment 1.2.1&quot;<br>          },<br>          {<br>            &quot;text&quot;: &quot;comment 1.2.2&quot;<br>          }<br>        ]<br>      }<br>    ]<br>  },<br>  {<br>    &quot;text&quot;: &quot;comment 2&quot;,<br>    &quot;children&quot;: []<br>  },<br>  {<br>    &quot;text&quot;: &quot;comment 3&quot;,<br>    &quot;children&quot;: []<br>  }<br>];</pre><h4>Comment Component</h4><p>Create a new comment component using given command</p><pre>ng g c components/comment</pre><p>Now in app.component.ts we will create a variable for comment schema.</p><p>Now we will add schema in comment component using <a href="https://angular.io/api/core/Input">Input Decorator</a>.</p><pre>@Input() comments: Array&lt;Comment&gt; | undefined;</pre><p>Now we will render nested comments in template. Copy and paste below code in comment.component.html or in template property of <a href="https://angular.io/api/core/Component">Component decorator</a>.</p><pre><a href="http://twitter.com/Component">@Component</a>({<br>  selector: &#39;app-comment&#39;,<br>  styleUrls: [&#39;./comment.component.scss&#39;],<br>  template: `<br>    &lt;div *ngFor=&quot;let comment of comments&quot;&gt;<br>      &lt;ul&gt;<br>       &lt;li&gt;<br>         {{comment.text}}<br>         &lt;app-comment [comments]=&quot;comment.children&quot; *ngIf=&quot;comment.children&quot;&gt;&lt;/app-comment&gt;<br>       &lt;/li&gt;<br>      &lt;/ul&gt;<br>    &lt;/div&gt;<br>  `<br>})</pre><p>Now we will get comment schema in app.component.ts file</p><pre>commentSchema: Array&lt;Comment&gt; = CommentSchema;</pre><p>Now we will call app-component in app.component.html file.</p><pre>&lt;app-comment [comments]=&quot;commentSchema&quot;&gt;&lt;/app-comment&gt;</pre><p>Thats it. If you will run your code it will look like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*u_jeghrHotQt8Rg1Jco14w.png" /><figcaption>Nested Comments</figcaption></figure><p>If you liked this please give a clap.</p><p>Also let us know who you are doing this in your project. Do share if you know any other approach.</p><blockquote><strong>In Next Article we will enhance this so that user can reply and like comments.</strong></blockquote><p>Let me know if you have any doubts.</p><p>Source Code is available on github: <a href="https://github.com/piyush303/recursively-use-angular-component">https://github.com/piyush303/recursively-use-angular-component</a></p><p>You can play here with this example: <a href="https://codesandbox.io/s/recursively-use-angular-component-nr7y62">https://codesandbox.io/s/recursively-use-angular-component-nr7y62</a></p><h4>Read Next</h4><ul><li><a href="https://jnpiyush.medium.com/how-to-use-different-environment-variables-in-angular-13-dd37b85a1ce4">How to use different Environment Variables in Angular 13</a></li><li><a href="https://jnpiyush.medium.com/how-to-set-up-jest-in-angular-app-961ddcbab8af">How to set up Jest in Angular App</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40591026117f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Deep Dive into CanDeactivate Route Guard]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://jnpiyush.medium.com/angular-deep-dive-into-candeactivate-route-guard-51b9e1b046da?source=rss-d63f3f458a46------2"><img src="https://cdn-images-1.medium.com/max/750/1*KpIqetK62QIfWrX5PeHtXA.jpeg" width="750"></a></p><p class="medium-feed-snippet">CanDeactivate Router guard is a browser event that&#x2019;s used to warn users that they&#x2019;re about to leave a page.</p><p class="medium-feed-link"><a href="https://jnpiyush.medium.com/angular-deep-dive-into-candeactivate-route-guard-51b9e1b046da?source=rss-d63f3f458a46------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://jnpiyush.medium.com/angular-deep-dive-into-candeactivate-route-guard-51b9e1b046da?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/51b9e1b046da</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Thu, 24 Mar 2022 09:40:57 GMT</pubDate>
            <atom:updated>2022-03-24T10:35:48.185Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How to set up Jest in Angular App]]></title>
            <link>https://jnpiyush.medium.com/how-to-set-up-jest-in-angular-app-961ddcbab8af?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/961ddcbab8af</guid>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Mon, 07 Mar 2022 06:13:11 GMT</pubDate>
            <atom:updated>2022-03-09T09:28:13.866Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*kCGm8oeJ3Qq_HDR2mcl1Dw.png" /></figure><h3>What is Unit Testing?</h3><p>Unit Testing is one of the many stages of <a href="https://theqalead.com/topics/types-software-testing/">software testing</a> and looks at single units, otherwise known as components, individually. This validates that each component of the software being tested works as it is designed to. Each unit is the smallest functional part of the software that can be tested and normally only has one input and one output.</p><p>Unit testing is done during the coding phase while the software or other product is being developed to make sure it is clear of bugs and ready before its release.</p><p>When we starting a new Angular application, the Angular CLI automatically sets up everything we need for unit testing using <a href="https://karma-runner.github.io/"><strong>Karma</strong></a> and <a href="https://jasmine.github.io/"><strong>Jasmine</strong></a>.</p><p>In this article we will learn how to ditch <a href="https://karma-runner.github.io/"><strong>Karma</strong></a> and <a href="https://jasmine.github.io/"><strong>Jasmine</strong></a><strong> </strong>and use <a href="https://jestjs.io/"><strong>Jest</strong></a> as our unit testing framework and runner.</p><p>Let’s begin.</p><h4>1. Create Angular App</h4><p>Generate angular app called angular-jest using given command.</p><pre>ng new angular-jest</pre><h4><strong>2. Install the dependencies</strong></h4><pre>npm install jest jest-preset-angular @types/jest @angular-builders/jest --save-dev</pre><p>or if you are using yarn as your package manager</p><pre>yarn add jest jest-preset-angular @types/jest @angular-builders/jest --dev</pre><h4>3. Configure Jest</h4><p>Now Create the jest.config.tsfile at the root of your project</p><pre><em>const</em> { pathsToModuleNameMapper } = require(&#39;ts-jest&#39;);<br><em>const</em> { compilerOptions } = require(&#39;./tsconfig&#39;);<br><br>module.exports = {<br>  preset: &#39;jest-preset-angular&#39;,<br>  roots: [&#39;&lt;rootDir&gt;/src/&#39;],<br>  testMatch: [&#39;**/+(*.)+(spec).+(ts)&#39;],<br>  setupFilesAfterEnv: [&#39;&lt;rootDir&gt;/src/test.ts&#39;],<br>  collectCoverage: true,<br>  coverageReporters: [&#39;html&#39;],<br>  coverageDirectory: &#39;coverage/angular-jest&#39;,<br>  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths || {}, {<br>    prefix: &#39;&lt;rootDir&gt;/&#39;<br>  })<br>};</pre><p>Locate to &lt;root-dir&gt;/src/test.ts file and replace entire content by following code.</p><pre><em>import</em> &#39;jest-preset-angular/setup-jest&#39;;<br><br>Object.defineProperty(window, &#39;CSS&#39;, {value: <em>null</em>});<br>Object.defineProperty(window, &#39;getComputedStyle&#39;, {<br>  value: () =&gt; {<br>    <em>return</em> {<br>      display: &#39;none&#39;,<br>      appearance: [&#39;-webkit-appearance&#39;]<br>    };<br>  }<br>});<br><br>Object.defineProperty(document, &#39;doctype&#39;, {<br>  value: &#39;&lt;!DOCTYPE html&gt;&#39;<br>});<br>Object.defineProperty(document.body.style, &#39;transform&#39;, {<br>  value: () =&gt; {<br>    <em>return</em> {<br>      enumerable: true,<br>      configurable: true<br>    };<br>  }<br>});</pre><p>update tsconfig.spec.json</p><pre>{<br>  &quot;extends&quot;<em>:</em> &quot;./tsconfig.json&quot;,<br>  &quot;compilerOptions&quot;<em>:</em> {<br>    &quot;outDir&quot;<em>:</em> &quot;./out-tsc/spec&quot;,<br>    &quot;types&quot;<em>:</em> [<br>      &quot;jest&quot;, // 1<br>      &quot;node&quot;<br>    ],<br>    &quot;esModuleInterop&quot;<em>:</em> true, // 2<br>    &quot;emitDecoratorMetadata&quot;<em>:</em> true // 3<br>  },<br>  &quot;files&quot;<em>:</em> [<br>    &quot;src/test.ts&quot;,<br>    &quot;src/polyfills.ts&quot;<br>  ],<br>  &quot;include&quot;<em>:</em> [<br>    &quot;src/**/*.spec.ts&quot;,<br>    &quot;src/**/*.d.ts&quot;<br>  ]<br>}</pre><p>remove complete test configuration from angular.json file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mZEdaoIRHK1wBiHPQErfuA.png" /></figure><p>add below script in your package.json under scripts</p><pre>&quot;test&quot;: &quot;jest&quot;</pre><p>Cool! now we have setup jest in our Angular application. Use below command to run your test.</p><pre>npm run test</pre><p>Now you should be able to see your test and it will also generate a coverage directory under root, where you can check coverage.</p><h4>4. Remove Karma Runner</h4><pre>npm uninstall karma karma-chrome-launcher karma-jasmine karma-jasmine-html-reporter karma-coverage</pre><p>or</p><pre>yarn remove karma karma-chrome-launcher karma-jasmine karma-jasmine-html-reporter karma-coverage</pre><p>Also remove karma.conf.js file from root of your project directory.</p><p>Hurrray !! we have setup jest in our Angular App.</p><p>If you liked this please give a clap.</p><p>Also let us know who you are doing this in your project. Do share if you know any other approach.</p><p><strong>you can find code on github</strong>: <a href="https://github.com/piyush303/angular-jest">https://github.com/piyush303/angular-jest</a></p><p>Sources:</p><p><a href="https://jestjs.io/">Jest</a></p><h4>Read Next</h4><ul><li><a href="https://jnpiyush.medium.com/how-to-create-custom-theme-in-angular-material-bd518818d2fc">How to create custom theme in Angular Material</a></li><li><a href="https://jnpiyush.medium.com/how-to-use-different-environment-variables-in-angular-13-dd37b85a1ce4">How to use different Environment Variables in Angular 13</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=961ddcbab8af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Change Component Prefix in Angular]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/bitsrc/how-to-change-component-prefix-in-angular-2e8088e5ce6d?source=rss-d63f3f458a46------2"><img src="https://cdn-images-1.medium.com/max/1200/1*LjVsrb7-i00Y3eNwxd9b5Q.jpeg" width="1200"></a></p><p class="medium-feed-snippet">How to change component prefix when creating new components in Angular</p><p class="medium-feed-link"><a href="https://medium.com/bitsrc/how-to-change-component-prefix-in-angular-2e8088e5ce6d?source=rss-d63f3f458a46------2">Continue reading on Bits and Pieces »</a></p></div>]]></description>
            <link>https://medium.com/bitsrc/how-to-change-component-prefix-in-angular-2e8088e5ce6d?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e8088e5ce6d</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Tue, 01 Mar 2022 12:50:56 GMT</pubDate>
            <atom:updated>2025-03-04T18:03:43.750Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How to use different Environment Variables in Angular 13]]></title>
            <link>https://jnpiyush.medium.com/how-to-use-different-environment-variables-in-angular-13-dd37b85a1ce4?source=rss-d63f3f458a46------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd37b85a1ce4</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Piyush Jain]]></dc:creator>
            <pubDate>Mon, 28 Feb 2022 13:10:42 GMT</pubDate>
            <atom:updated>2022-03-08T14:17:46.825Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Angular 13 Environment Variable configuration" src="https://cdn-images-1.medium.com/max/1024/1*Npvz8yQ1tq5nkmNPr5wIRA.png" /><figcaption>Source — <a href="https://www.elite-corner.com/wp-content/uploads/2020/06/Environment-Variable-Configuration-in-Angular-Application.png">https://www.elite-corner.com/wp-content/uploads/2020/06/Environment-Variable-Configuration-in-Angular-Application.png</a></figcaption></figure><h4>Introduction</h4><p>In this article we will learn how to use different environments files for different environments.</p><p>When we create angular app using cli by default it has 2 environment files one for dev and one for prod.</p><p>Sometimes we need to create separate files for different environments e.g. QA, UAT, Staging, Prod etc. when we have separate API URLS for different environment.</p><p>Let’s deep dive into this.</p><h4>Custom environment file</h4><p>For each target environment, we will create a new environment file under folder environments. For demo, I have created separate environment files for qa and uat environments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*ZUVD4PAV2jKT2WyweKC2AA.png" /></figure><h4>Custom environment settings</h4><p>You can put your API URL and other settings in custom environment file. For example,</p><pre>export const environment = {<br>  production: true,<br>  envName: &#39;UAT&#39;,<br>  apiUrl: &#39;https://uat.endpoint.com&#39;<br>};</pre><h4>Update angular.json configuration</h4><p>In your Angular project, you shall find angular.json file. Inside this file, you can add a new section under projects/architect/build section.</p><p>Here is an example with uat and qa environments.</p><pre>&quot;uat&quot;: {<br>  &quot;fileReplacements&quot;: [<br>    {<br>      &quot;replace&quot;: &quot;src/environments/environment.ts&quot;,<br>      &quot;with&quot;: &quot;src/environments/environment.uat.ts&quot;<br>    }<br>  ],<br>  &quot;outputHashing&quot;: &quot;all&quot;<br>}</pre><pre>&quot;qa&quot;: {<br>  &quot;fileReplacements&quot;: [<br>    {<br>      &quot;replace&quot;: &quot;src/environments/environment.ts&quot;,<br>      &quot;with&quot;: &quot;src/environments/environment.qa.ts&quot;<br>    }<br>  ],<br>  &quot;outputHashing&quot;: &quot;all&quot;<br>}</pre><h4>Build application for custom environment</h4><p>Once your configuration is done, you can build your application for targeted environment by specific --configuration parameter to ng build command. For example, to build application in QA mode, run command</p><p>ng build --configuration qa</p><p>and dist folder shall contains build output with qa configuration. You can publish your website on your target environment now.</p><h4>Github</h4><p><a href="https://github.com/piyush303/angular-configure-different-environments">GitHub - piyush303/angular-configure-different-environments: angular-configure-different-environments</a></p><p>If you liked this please give a clap.</p><p>Also let us know who you are doing this in your project. Do share if you know any other approach.</p><h4>Read Next</h4><ul><li><a href="https://jnpiyush.medium.com/how-to-build-reusable-table-component-in-angular-7a7ce79d2754">How to build Reusable Table Component in Angular</a></li><li><a href="https://jnpiyush.medium.com/how-to-set-up-jest-in-angular-app-961ddcbab8af">How to set up Jest in Angular App</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd37b85a1ce4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>