Bird
Raised Fist0
Angularframework~20 mins

Container and presentational components in Angular - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Angular Container & Presentational Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Identify the role of the container component

In Angular, a container component is responsible for managing data and logic, while a presentational component focuses on displaying UI. Given the following scenario, which behavior best describes a container component?

Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-user-container',
  standalone: true,
  template: `<app-user-profile [user]="user()"></app-user-profile>`,
  imports: [UserProfileComponent]
})
export class UserContainerComponent {
  user = signal({ name: 'Alice', age: 30 });
}
AOnly displays static user information without any data logic
BHandles UI styling and layout without managing data
CManages user data and passes it down to the presentational component
DDirectly modifies the DOM elements for user display
Attempts:
2 left
💡 Hint

Think about which component holds the data and which one just shows it.

state_output
intermediate
2:00remaining
What is the output of the presentational component?

Given a presentational component that receives a user object as input and displays the user's name, what will be rendered?

Angular
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-user-profile',
  standalone: true,
  template: `<p>User: {{ user.name }}</p>`
})
export class UserProfileComponent {
  @Input() user!: { name: string; age: number };
}

// Usage in container template:
// <app-user-profile [user]="{ name: 'Bob', age: 25 }"></app-user-profile>
A<p>User: undefined</p>
B<p>User: { name: 'Bob', age: 25 }</p>
CNo output due to missing user input
D<p>User: Bob</p>
Attempts:
2 left
💡 Hint

Look at how the template accesses the user property.

lifecycle
advanced
2:00remaining
When does the container component update the presentational component?

In Angular standalone components using signals, when does the presentational component update its display after the container component changes the data?

Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-container',
  standalone: true,
  template: `<app-display [data]="data()"></app-display>`,
  imports: [DisplayComponent]
})
export class ContainerComponent {
  data = signal('initial');

  updateData() {
    this.data.set('updated');
  }
}
AImmediately after the signal changes, Angular re-renders the presentational component
BOnly after a manual change detection is triggered
CWhen the user clicks a button inside the presentational component
DAfter the container component is destroyed and recreated
Attempts:
2 left
💡 Hint

Signals automatically notify Angular when their value changes.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a presentational component?

Choose the correct Angular standalone presentational component that accepts an input and displays it.

A
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-display',
  standalone: true,
  template: `&lt;p&gt;{{ text }}&lt;/p&gt;`
})
export class DisplayComponent {
  @Input() text!: string;
}
B
import { Component } from '@angular/core';

@Component({
  selector: 'app-display',
  template: `&lt;p&gt;{{ text }}&lt;/p&gt;`
})
export class DisplayComponent {
  text: string = '';
}
C
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-display',
  standalone: true,
  template: `&lt;p&gt;{{ text }}&lt;/p&gt;`
})
export class DisplayComponent {
  text: string = '';
}
D
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-display',
  standalone: true,
  template: `&lt;p&gt;{{ text }}&lt;/p&gt;`
})
export class DisplayComponent {
  @Input text: string;
}
Attempts:
2 left
💡 Hint

Check the syntax for declaring inputs and standalone components.

🔧 Debug
expert
3:00remaining
Why does the presentational component not update when container data changes?

Given this container and presentational component setup, why does the UI not update when the container changes the data?

Angular
import { Component, signal, Input } from '@angular/core';

@Component({
  selector: 'app-container',
  standalone: true,
  template: `<app-display [data]="data"></app-display>`,
  imports: [DisplayComponent]
})
export class ContainerComponent {
  data = signal('hello');

  changeData() {
    this.data.set('world');
  }
}

@Component({
  selector: 'app-display',
  standalone: true,
  template: `<p>{{ data }}</p>`
})
export class DisplayComponent {
  @Input() data!: string;
}
AThe presentational component template uses incorrect interpolation syntax
BThe container passes the signal itself instead of its value, so the presentational component does not detect changes
CThe container component does not call changeData() to update the signal
DThe presentational component is missing the @Input decorator
Attempts:
2 left
💡 Hint

Check what is passed as input and what the presentational component expects.

Practice

(1/5)
1. In Angular, what is the main role of a container component?
easy
A. To manage CSS and animations
B. To display UI elements and styles
C. To define routes and navigation
D. To handle data fetching and business logic

Solution

  1. Step 1: Understand container component responsibility

    Container components are designed to manage data and logic, such as fetching data or handling user actions.
  2. Step 2: Differentiate from presentational components

    Presentational components focus on showing data and UI, not on data handling.
  3. Final Answer:

    To handle data fetching and business logic -> Option D
  4. Quick Check:

    Container = data and logic [OK]
Hint: Container = data and logic, Presentational = UI only [OK]
Common Mistakes:
  • Confusing container with presentational component roles
  • Thinking container manages styles or routes
  • Assuming container handles only UI display
2. Which of the following is the correct way to pass data from a container to a presentational component in Angular?
easy
A. Use ngModel in container component only
B. @Input() data: any; in presentational, bind in container template
C. @Output() data: any; in presentational, bind in container template
D. Directly modify presentational component's variables from container

Solution

  1. Step 1: Identify Angular data flow syntax

    Data flows from container to presentational via @Input() properties.
  2. Step 2: Understand binding in container template

    The container passes data by binding to the presentational component's input property in its template.
  3. Final Answer:

    @Input() data: any; in presentational, bind in container template -> Option B
  4. Quick Check:

    Data down via @Input() [OK]
Hint: Use @Input() to pass data down from container [OK]
Common Mistakes:
  • Using @Output() to pass data down instead of events up
  • Trying to modify child variables directly
  • Confusing ngModel with input binding
3. Given this Angular container component template:
<app-user-list [users]="userArray" (selectUser)="onUserSelect($event)"></app-user-list>

What is the role of (selectUser) here?
medium
A. It defines a CSS class for styling
B. It binds data from container to presentational component
C. It listens to an event emitted by the presentational component
D. It initializes the component's state

Solution

  1. Step 1: Recognize Angular event binding syntax

    Parentheses around selectUser indicate event binding from child to parent.
  2. Step 2: Understand event emission from presentational component

    The presentational component emits selectUser event, container listens and runs onUserSelect.
  3. Final Answer:

    It listens to an event emitted by the presentational component -> Option C
  4. Quick Check:

    Parent listens to child event with (event) [OK]
Hint: Parent uses (event) to listen to child events [OK]
Common Mistakes:
  • Thinking (selectUser) passes data down
  • Confusing event binding with property binding
  • Assuming it styles or initializes state
4. What is wrong with this presentational component code snippet?
@Component({
  selector: 'app-item',
  template: `<div>{{item.name}}</div>`
})
export class ItemComponent {
  item: any;
}
medium
A. Missing @Input() decorator on item property
B. Template syntax is incorrect
C. Selector name is invalid
D. Component class should be abstract

Solution

  1. Step 1: Check data input declaration

    The presentational component expects data from parent, so item must be decorated with @Input() to receive it.
  2. Step 2: Verify template and selector

    Template syntax and selector are valid; no abstract class needed.
  3. Final Answer:

    Missing @Input() decorator on item property -> Option A
  4. Quick Check:

    @Input() needed to receive data [OK]
Hint: Add @Input() to receive data in presentational component [OK]
Common Mistakes:
  • Forgetting @Input() on input properties
  • Thinking template interpolation is wrong
  • Assuming selector must be different
5. You want to create a container component that fetches a list of products and passes it to a presentational component for display. Which approach best follows Angular container and presentational component patterns?
hard
A. Container fetches products, stores in a variable, passes via @Input(); presentational only displays list
B. Presentational component fetches products and emits events to container
C. Container and presentational both fetch products independently
D. Presentational component manages fetching and state internally

Solution

  1. Step 1: Identify container responsibility

    The container should handle fetching data and managing state.
  2. Step 2: Identify presentational responsibility

    The presentational component should only display data received via @Input() without fetching or managing state.
  3. Final Answer:

    Container fetches products, stores in a variable, passes via @Input(); presentational only displays list -> Option A
  4. Quick Check:

    Container = data fetch, Presentational = display [OK]
Hint: Container fetches data, presentational displays it [OK]
Common Mistakes:
  • Letting presentational fetch data
  • Duplicating data fetch in both components
  • Mixing data logic inside presentational