← Back to Angular, page 4

angular-0065 · Easy fresher

What are smart (container) and presentational (dumb) components, and why does Angular code often split them?

A container component knows where data comes from (services, routes, stores) and passes it down. A presentational component only receives inputs, renders them, and emits outputs, so it is easy to reuse and test. The split keeps logic and display apart and works well with OnPush.

Full answer

Large components that fetch data, hold state, and draw every detail become hard to read and test. A common remedy is to split them into two kinds.

Container (smart) components:

  • Inject services, read route data, and talk to stores or APIs.
  • Hold state and decide what to do with events.
  • Contain little markup. Mostly they render presentational components and pass inputs.
  • Are usually specific to one page or feature.

Presentational (dumb) components:

  • Get everything through input() and communicate through output().
  • Do not inject feature services or know about HTTP and routing.
  • Often use ChangeDetectionStrategy.OnPush, since their inputs fully describe their output.
  • Are reusable (a student card, a pager, a price tag) and easy to test: set inputs, check the DOM, check emitted events.

A classroom example: the class teacher (container) collects marks from the exam office (service) and hands each parent (presentational card) a printed report. The card does not call the exam office. It only shows what it was given, and if a parent presses "Request review", the card sends that request back to the teacher.

Benefits: tests are small, the display can change without touching the data logic, and designers can preview presentational components in isolation (for example in Storybook).

Cautions: do not split blindly. A small feature with one component is fine. Too many layers produce files full of inputs and outputs passed through (prop drilling), which a shared service can solve. Today, signals and inject() make it easy to keep containers thin.

typescript
import { ChangeDetectionStrategy, Component, Injectable, input, output, signal } from '@angular/core';

export interface Result {
  id: number;
  student: string;
  marks: number;
}

@Injectable({ providedIn: 'root' })
export class ResultService {
  readonly results = signal<Result[]>([
    { id: 1, student: 'Asha', marks: 88 },
    { id: 2, student: 'Imran', marks: 52 },
  ]);
}

// Presentational: inputs and outputs only.
@Component({
  selector: 'app-result-card',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <p>{{ result().student }}: {{ result().marks }}</p>
    <button (click)="review.emit(result().id)">Request review</button>
  `,
})
export class ResultCardComponent {
  result = input.required<Result>();
  review = output<number>();
}

// Container: injects the service and handles events.
@Component({
  selector: 'app-results-page',
  imports: [ResultCardComponent],
  template: `
    @for (r of service.results(); track r.id) {
      <app-result-card [result]="r" (review)="onReview($event)" />
    }
    <p>Review requests: {{ requested().length }}</p>
  `,
})
export class ResultsPageComponent {
  readonly requested = signal<number[]>([]);

  constructor(protected readonly service: ResultService) {}

  onReview(id: number): void {
    this.requested.update((list) => [...list, id]);
  }
}

Angular 20.3, strict template check passes. The card has no injected services, so its test needs only inputs.

Key points

  • Container components own data and decisions. Presentational components receive inputs and emit outputs.
  • Presentational components suit OnPush and are easy to test.
  • The split improves reuse but costs extra files. Use it where it pays off.
  • Avoid long chains of passed-through inputs. Use a service for that.

Common mistakes

  • Injecting an HTTP service into a card component that should be reusable.
  • Splitting every tiny component and creating prop drilling.
  • Letting presentational components mutate input objects.

Follow-ups

  • How would you test each kind of component?
  • Where would route parameters be read?
  • How does this pattern fit with a signal store?

Tips

  • Explain with one realistic example, and say how you would test each part.
  • State the cost of the pattern so it does not sound like a rule for everything.
  • Mention OnPush as a natural fit.

Evaluation rubric

  • Describes responsibilities of both kinds.
  • Names benefits and costs.
  • Connects the pattern to testing and OnPush.