Performance: Container and presentational components
MEDIUM IMPACT
This pattern affects rendering speed and responsiveness by separating data logic from UI rendering, reducing unnecessary DOM updates.
import { Component, signal, Input } from '@angular/core'; @Component({ selector: 'app-user-container', standalone: true, template: `<app-user-presentational [user]="user()"></app-user-presentational>` }) export class UserContainerComponent { user = signal(null); constructor() { fetch('/api/user') .then(res => res.json()) .then(data => this.user.set(data)); } } @Component({ selector: 'app-user-presentational', standalone: true, template: `<div>{{ user?.name }}</div>` }) export class UserPresentationalComponent { @Input() user: any | null = null; }
import { Component } from '@angular/core'; @Component({ selector: 'app-user', template: `<div>{{ user?.name }}</div>` }) export class UserComponent { user = null; constructor() { fetch('/api/user') .then(res => res.json()) .then(data => this.user = data); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Combined data + UI component | High (full component tree updates) | Multiple reflows per data change | High paint cost due to large updates | [X] Bad |
| Container + presentational components | Low (only presentational updates) | Single reflow per data change | Low paint cost focused on UI parts | [OK] Good |