Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Smart and Dumb Component Pattern in Angular
📖 Scenario: You are building a simple Angular app to display a list of books. You want to separate the logic and data handling from the display. This helps keep your code clean and easy to manage.
🎯 Goal: Create a smart component that holds the book data and a dumb component that only shows the book titles. The smart component will pass the book list to the dumb component.
📋 What You'll Learn
Create a smart component called BookListComponent with a list of books
Create a dumb component called BookDisplayComponent that receives the list of books as an input
Use Angular's @Input() decorator in the dumb component to accept the book list
Display the book titles in the dumb component using an *ngFor loop
Use standalone components and Angular 17+ syntax with signals and new control flow directives
💡 Why This Matters
🌍 Real World
Separating smart and dumb components helps keep Angular apps organized and easier to maintain, especially as they grow.
💼 Career
Understanding this pattern is important for Angular developers to write clean, reusable, and testable code in professional projects.
Progress0 / 4 steps
1
Create the smart component with book data
Create a standalone Angular component called BookListComponent. Inside it, create a signal called books that holds this array of strings: ["The Hobbit", "1984", "Pride and Prejudice"].
Angular
Hint
Use signal to create a reactive variable holding the book titles array.
2
Create the dumb component to display books
Create a standalone Angular component called BookDisplayComponent. Add an @Input() property called books of type string[]. The template should use *ngFor to show each book title inside a <li> element.
Angular
Hint
Use @Input() to receive data and *ngFor to loop over the books array.
3
Connect the smart component to the dumb component
In the BookListComponent template, add the <app-book-display> tag. Bind the books signal value to the dumb component's books input using Angular's property binding syntax.
Angular
Hint
Use [books]="books()" to bind the signal value to the dumb component input.
4
Add accessibility and finalize the dumb component
In the BookDisplayComponent template, add role="list" to the <ul> and role="listitem" to each <li>. This improves accessibility for screen readers.
Angular
Hint
Add role="list" to the <ul> and role="listitem" to each <li> for better accessibility.
Practice
(1/5)
1. What is the main role of a smart component in Angular's smart and dumb component pattern?
Assuming missing decorators cause runtime error here
Ignoring event binding from dumb to smart
4. Identify the error in this dumb component code that prevents it from emitting events to the smart component:
@Component({
selector: 'app-dumb',
template: `<button (click)="clicked.emit()">Click</button>`
})
export class DumbComponent {
clicked = new EventEmitter<void>();
}
medium
A. EventEmitter should be imported from '@angular/core/testing'
B. Missing @Output() decorator on the clicked property
C. The template syntax for click event is incorrect
D. The clicked property should be a function, not EventEmitter
Solution
Step 1: Check event emitter declaration
The clicked property must have @Output() decorator to emit events to parent.
Step 2: Verify imports and syntax
EventEmitter is correctly imported from '@angular/core', template syntax is correct.
Final Answer:
Missing @Output() decorator on the clicked property -> Option B
Quick Check:
@Output() missing = no event emission [OK]
Hint: Always add @Output() to EventEmitter properties [OK]
Common Mistakes:
Forgetting @Output() decorator
Importing EventEmitter from wrong package
Miswriting template event binding syntax
5. You want to refactor a large Angular component that mixes data fetching, logic, and UI display into smart and dumb components. Which approach best follows the smart and dumb component pattern?
hard
A. Create a smart component to fetch data and handle logic, pass data via @Input() to dumb components that only display UI and emit events
B. Move all logic and data fetching into dumb components and keep smart components only for styling
C. Combine smart and dumb components into one to reduce complexity
D. Use dumb components to fetch data and smart components to display UI
Solution
Step 1: Separate concerns by responsibility
Smart components should handle data fetching and logic, dumb components focus on UI and user interaction.
Step 2: Use Angular bindings correctly
Pass data from smart to dumb via @Input() and receive events via @Output().
Final Answer:
Create a smart component to fetch data and handle logic, pass data via @Input() to dumb components that only display UI and emit events -> Option A
Quick Check:
Smart = logic/data, Dumb = UI only [OK]
Hint: Smart handles data/logic; dumb handles UI and events [OK]