0
0
Angularframework~10 mins

Smart and dumb component pattern in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Smart and dumb component pattern
Smart Component
Fetches Data
Passes Data as Inputs
Dumb Component
Displays Data
Emits Events
Smart Component Handles Events
Updates Data or State
Smart components get data and handle logic, then pass data to dumb components. Dumb components show data and send events back.
Execution Sample
Angular
import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({ selector: 'dumb', template: `<button (click)="clicked()">{{label}}</button>` })
export class DumbComponent {
  @Input() label = '';
  @Output() action = new EventEmitter<void>();
  clicked() { this.action.emit(); }
}
A dumb component shows a button with a label and emits an event when clicked.
Execution Table
StepComponentActionData/State BeforeEvent EmittedData/State After
1SmartInitialize with dataitems = []Noneitems = ['Save', 'Cancel']
2SmartPass data to Dumbitems = ['Save', 'Cancel']NoneDumb receives label='Save'
3DumbRender buttonlabel='Save'NoneButton shows 'Save'
4UserClicks buttonButton shows 'Save'Click eventNone
5DumbEmit action eventClickedaction.emit()None
6SmartHandle eventitems = ['Save', 'Cancel']Received action eventPerform save logic
7SmartUpdate stateAfter saveNoneitems updated or UI refreshed
8SmartPass updated dataUpdated itemsNoneDumb receives new label if changed
9DumbRender updated buttonNew labelNoneButton shows updated label
10EndNo more actionsFinal stateNoneUI stable
💡 User stops interacting; smart component manages data and dumb component only displays and emits events.
Variable Tracker
VariableStartAfter 1After 6After 7Final
items[]['Save', 'Cancel']['Save', 'Cancel']['Save', 'Cancel']['Save', 'Cancel']
label'''Save''Save''Save''Save'
eventEmittedfalsefalsetruefalsefalse
Key Moments - 3 Insights
Why does the dumb component not fetch or change data?
Because the dumb component only receives data via inputs and emits events; it does not handle data fetching or logic. See execution_table steps 2 and 5.
How does the smart component know when to update data?
The dumb component emits an event when user interacts (step 5), and the smart component listens and updates data accordingly (step 6).
Why pass data down and events up instead of sharing data directly?
This keeps dumb components simple and reusable, and smart components control data flow and logic, improving separation of concerns.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the label value passed to the dumb component at step 2?
A'Cancel'
B'Save'
C'' (empty string)
D['Save', 'Cancel']
💡 Hint
Check the 'Data/State After' column at step 2 where dumb receives label='Save'.
At which step does the dumb component emit an event to the smart component?
AStep 5
BStep 6
CStep 3
DStep 8
💡 Hint
Look for 'Emit action event' in the 'Action' column in execution_table.
If the smart component updates the label after handling the event, which step shows the dumb component rendering the updated label?
AStep 4
BStep 7
CStep 9
DStep 10
💡 Hint
Step 9 shows 'Render updated button' with new label in execution_table.
Concept Snapshot
Smart and dumb components pattern in Angular:
- Smart components fetch and manage data.
- Dumb components receive data via @Input and display it.
- Dumb components emit events via @Output.
- Smart components listen to events and update data.
- This separates logic (smart) from UI (dumb) for clarity and reuse.
Full Transcript
The smart and dumb component pattern splits responsibilities in Angular apps. Smart components handle data fetching and logic. They pass data down to dumb components using inputs. Dumb components only display data and emit events when users interact. The smart component listens to these events and updates data or state accordingly. This keeps dumb components simple and reusable, while smart components control the app's behavior. The execution table shows the smart component initializing data, passing it to dumb, dumb rendering and emitting events, and smart handling those events to update state. Variables like items and label change as data flows. This pattern helps beginners understand clear separation of concerns in Angular components.