Bird
0
0

What is wrong with this Angular component code snippet that tries to avoid NgRx?

medium📝 Debug Q7 of 15
Angular - State Management
What is wrong with this Angular component code snippet that tries to avoid NgRx?
export class ExampleComponent {
  state = { value: 0 };
  increment() {
    this.state.value++;
  }
}

{{ state.value }}

AIncrement method should not modify state
BDirectly mutating state object can cause change detection issues
Cstate should be a primitive, not an object
DMissing @Input decorator on state
Step-by-Step Solution
Solution:
  1. Step 1: Understand Angular change detection

    Angular detects changes by object reference, not deep mutation.
  2. Step 2: Identify mutation issue

    Incrementing state.value mutates the object without changing reference, may not update view.
  3. Final Answer:

    Directly mutating state object can cause change detection issues -> Option B
  4. Quick Check:

    Mutate immutably to trigger Angular updates [OK]
Quick Trick: Avoid mutating objects directly; create new copies [OK]
Common Mistakes:
  • Assuming mutation always triggers view update
  • Thinking primitives are required for state
  • Confusing @Input with local state

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes