Performance: Container and presentational components
This pattern affects rendering speed and responsiveness by separating data logic from UI rendering, reducing unnecessary DOM updates.
Jump into concepts and practice - no test required
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 |
@Input() properties.<app-user-list [users]="userArray" (selectUser)="onUserSelect($event)"></app-user-list>
(selectUser) here?selectUser indicate event binding from child to parent.selectUser event, container listens and runs onUserSelect.@Component({
selector: 'app-item',
template: `<div>{{item.name}}</div>`
})
export class ItemComponent {
item: any;
}item must be decorated with @Input() to receive it.@Input() without fetching or managing state.