Bird
0
0

You want to create a signal that holds a list of numbers and add a new number to it reactively. Which code correctly updates the signal to add number 7?

hard📝 component behavior Q15 of 15
Angular - State Management
You want to create a signal that holds a list of numbers and add a new number to it reactively. Which code correctly updates the signal to add number 7?
Anumbers.push(7);
Bnumbers.set(numbers() + 7);
Cnumbers = signal([...numbers(), 7]);
Dnumbers.update(list => [...list, 7]);
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to update array signals

    Signals hold values immutably; to add an item, create a new array with the old items plus the new one.
  2. Step 2: Check each option's correctness

    numbers.update(list => [...list, 7]); uses update() with a function that returns a new array including 7, which is correct.
    numbers.set(numbers() + 7); tries to add 7 to an array directly, causing a type error.
    numbers = signal([...numbers(), 7]); tries to reassign the signal variable, which is incorrect.
    numbers.push(7); calls push() on the signal itself, which is not valid.
  3. Final Answer:

    numbers.update(list => [...list, 7]); -> Option D
  4. Quick Check:

    Use update() with function returning new array [OK]
Quick Trick: Use update(fn) to immutably add items to array signals [OK]
Common Mistakes:
  • Trying to mutate signal value directly
  • Reassigning signal variable instead of updating
  • Using set() with wrong value type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes