Bird
0
0

You have multiple subscriptions in a component. Which is the best way to clean them up in ngOnDestroy?

hard📝 Conceptual Q8 of 15
Angular - Lifecycle Hooks
You have multiple subscriptions in a component. Which is the best way to clean them up in ngOnDestroy?
AUse a Subscription container and call unsubscribe once in ngOnDestroy
BCall unsubscribe on each subscription separately in ngOnDestroy
CDo not unsubscribe; Angular handles it automatically
DSet subscriptions to null in ngOnDestroy
Step-by-Step Solution
Solution:
  1. Step 1: Understand managing multiple subscriptions

    Managing many subscriptions individually is error-prone and verbose.
  2. Step 2: Use Subscription container

    Subscription container (like Subscription.add or Subscription[]) allows grouping subscriptions and unsubscribing all at once.
  3. Final Answer:

    Use a Subscription container and call unsubscribe once in ngOnDestroy -> Option A
  4. Quick Check:

    Group subscriptions for single unsubscribe call [OK]
Quick Trick: Group subscriptions to unsubscribe once in ngOnDestroy [OK]
Common Mistakes:
  • Unsubscribing each subscription manually
  • Assuming Angular auto-unsubscribes
  • Setting subscriptions to null without unsubscribing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes