Performance: Smart and dumb component pattern
This pattern affects rendering speed and interaction responsiveness by separating data handling from UI rendering.
Jump into concepts and practice - no test required
import { Component, Input } from '@angular/core'; @Component({ selector: 'app-user-display', template: `<div>{{user?.name}}</div>` }) export class UserDisplayComponent { @Input() user: any; } @Component({ selector: 'app-user-container', template: `<app-user-display [user]="user"></app-user-display>` }) export class UserContainerComponent { user = null; constructor() { fetch('https://api.example.com/user') .then(res => res.json()) .then(data => this.user = data); } }
import { Component } from '@angular/core'; @Component({ selector: 'app-user', template: `<div>{{user?.name}}</div>` }) export class UserComponent { user = null; constructor() { fetch('https://api.example.com/user') .then(res => res.json()) .then(data => this.user = data); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Smart and dumb components separated | Minimal DOM updates in dumb components | Single reflow on data change | Low paint cost due to isolated updates | [OK] Good |
| Combined data fetching and UI rendering | Full component DOM updates | Multiple reflows on data fetch | Higher paint cost due to full re-render | [X] Bad |
smart component in Angular's smart and dumb component pattern?@Input() properties to receive data from parents./* Smart component template */
<app-dumb [title]="pageTitle" (clicked)="onClicked()"></app-dumb>
/* Smart component class */
pageTitle = 'Hello World';
onClicked() { console.log('Clicked!'); }
/* Dumb component template */
<h1>{{ title }}</h1>
<button (click)="clicked.emit()">Click Me</button>
/* Dumb component class */
@Input() title: string;
@Output() clicked = new EventEmitter<void>();[title] input, so dumb displays it.clicked event on button click, smart component listens and logs 'Clicked!'.@Component({
selector: 'app-dumb',
template: `<button (click)="clicked.emit()">Click</button>`
})
export class DumbComponent {
clicked = new EventEmitter<void>();
}clicked property must have @Output() decorator to emit events to parent.@Input() and receive events via @Output().