How to Dockerize Your React App for Scalable Deployment

A leading software development company specializing in web and mobile app solutions. We started our journey in 2011 and since then it has been quite a success. We believe in consistently driving and delivering innovation. We prioritize delivering software products that address your unique business needs. Our software product engineering process is designed to provide a seamless and collaborative experience.
Pipes are a cornerstone of Angular development, allowing you to transform data within templates for improved readability and user experience. However, not all pipes are created equal. This blog delves into the key differences between pure and impure pipes in Angular, exploring their characteristics, use cases, and performance implications.
Pipes in Angular are essentially functions that take data as input and manipulate it into a desired output. Angular provides a rich set of built-in pipes for common tasks like formatting dates (DatePipe), currencies (CurrencyPipe), and text casing (UpperCasePipe, LowerCasePipe). Additionally, you have the flexibility to create custom pipes for specific transformations.
Pipes are typically invoked using the pipe symbol (|
) within your template. You can even chain multiple pipes to achieve a desired outcome. Here's an example:
<p>Today is {{ today | date:'fullDate' }}</p>
<p>Price is {{ price | currency: 'USD' }}</p>
In this instance, the date
pipe transforms the today
value into a more human-readable format ("Thursday, September 19, 2024"), while the currency
pipe converts the price
value into US dollars ("$4300").
Pure pipes represent the default type in Angular. They offer several key characteristics:
Here’s a basic example of a custom pure pipe that converts text to uppercase:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'pureUppercase'
})
export class PureUppercasePipe implements PipeTransform {
transform(value: string): string {
console.log('Pure Pipe Called');
return value.toUpperCase();
}
}
In the template:
<p>{{ 'hello world' | pureUppercase }}</p>
The pureUppercase
pipe transforms "hello world" to "HELLO WORLD". Since it's pure, it only re-executes if the input string changes.
Use Cases for Pure Pipes:
An example of a built-in pure pipe is the TitlecasePipe
, which capitalizes the first letter of each word.
Impure pipes, as the name suggests, are recalculated on every change detection cycle, regardless of input data changes. This means Angular calls the pipe’s transform
method whenever something happens within the application, even if the pipe's input hasn't changed.
Characteristics of Impure Pipes:
pure
property to false
in the pipe's decorator.Example of an Impure Pipe:
Here’s a pipe that filters an array based on a search string:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'impureFilter',
pure: false
})
export class ImpureFilterPipe implements PipeTransform
{
transform(items: any[], searchText: string): any[] {
if (!items || !searchText) {
return items;
}
return items.filter(
item => item.name.toLowerCase().includes(searchText.toLowerCase())
);
}
}
In the template:
<ul>
<li *ngFor="let item of items | impureFilter:searchText">{{ item.name }}</li>
</ul>
The impureFilter
pipe filters the items
array based on the searchText
. Since it's impure, it re-exec
Use Cases for Impure Pipes:
An example of a built-in impure pipe is the JsonPipe
, which serializes objects into JSON strings for display in the template.
Working with Immutable Data in Pure and Impure Pipes
Pure and impure pipes behave differently when working with immutable data types like arrays and objects.
<div>Age: {{ user.birthDate | agePipe }}</div>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent
{
user = { name: 'Rohit', birthDate: '12-02-1998' };
}
function updateUser() {
this.user.birthDate = '12-05-2002'; // Update the birthDate property
}
In this example, we’re updating the birthDate
property of the user
object directly (without changing reference).
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'agePipe',
pure: true
})
export class AgePipe implements PipeTransform {
transform(dateString:
string): number {
const today = new Date();
const birthDate = new Date(dateString);
const age = today.getFullYear() - birthDate.getFullYear();
return age;
}
}
Since the pipe is pure, directly mutating the object won’t trigger recalculation. To force a change detection update, you need to reassign the user
object with a new reference:
function updateUser() {
this.user = {
...this.user,
birthDate: '12-05-2002'
};
}
2. Impure Pipe:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'agePipe',
pure: false
})
export class AgePipe implements PipeTransform {
transform(dateString: string): number {
const today = new Date();
const birthDate = new Date(dateString);
const age = today.getFullYear() - birthDate.getFullYear();
return age;
}
}
With an impure pipe, mutating the object directly will trigger recalculation.
Pure Pipes:
Impure Pipes:
By carefully choosing between pure and impure pipes, you can optimize both the performance and functionality of your Angular application. Pure pipes are generally preferred for their efficiency, while impure pipes are necessary for specific use cases involving dynamic or mutable data.
To effectively implement pure pipes in Angular, consider hiring expert Angular developers from Angular Minds.
Our team possesses the knowledge and experience to optimize your application’s performance and ensure seamless integration of pure pipes.
Read detailed blog here:- https://www.angularminds.com/blog/pure-vs-impure-pipes-in-angular
Comments
Post a Comment