0
0
Angularframework~20 mins

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

Choose your learning style9 modes available
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.