0
0
Svelteframework~20 mins

Array and object reactivity gotchas in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Reactivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Why does this Svelte component not update the list display?

Consider this Svelte component that tries to add an item to an array and show the updated list:

  <script>
    let items = ['apple', 'banana'];
    function addItem() {
      items.push('orange');
    }
  </script>

  <button on:click={addItem}>Add Orange</button>
  <ul>
    {#each items as item}
      <li>{item}</li>
    {/each}
  </ul>

Why does clicking the button not update the displayed list?

Svelte
  <script>
    let items = ['apple', 'banana'];
    function addItem() {
      items.push('orange');
    }
  </script>

  <button on:click={addItem}>Add Orange</button>
  <ul>
    {#each items as item}
      <li>{item}</li>
    {/each}
  </ul>
ABecause Svelte does not detect changes when mutating arrays directly; the array reference must change to trigger reactivity.
BBecause the push method is asynchronous and the UI updates before the item is added.
CBecause the items array is declared with let instead of const, so Svelte ignores changes.
DBecause the {#each} block requires a key to detect changes in the array.
Attempts:
2 left
💡 Hint

Think about how Svelte tracks changes to variables and when it updates the UI.

state_output
intermediate
2:00remaining
What is the value of 'user.name' after this update in Svelte?

Given this Svelte code:

  <script>
    let user = { name: 'Alice', age: 30 };
    function updateName() {
      user.name = 'Bob';
    }
  </script>

  <button on:click={updateName}>Change Name</button>
  <p>Name: {user.name}</p>

After clicking the button, what will be displayed as the name?

Svelte
  <script>
    let user = { name: 'Alice', age: 30 };
    function updateName() {
      user.name = 'Bob';
    }
  </script>

  <button on:click={updateName}>Change Name</button>
  <p>Name: {user.name}</p>
AThe component will throw an error
BBob
Cundefined
DAlice
Attempts:
2 left
💡 Hint

Consider how Svelte detects changes in object properties.

📝 Syntax
advanced
2:00remaining
Which option correctly updates an array reactively in Svelte?

Choose the code snippet that correctly updates the todos array so Svelte reacts and updates the UI:

Atodos.push('new task');
Btodos = [...todos, 'new task'];
Ctodos = todos.concat('new task');
Dtodos[todos.length] = 'new task';
Attempts:
2 left
💡 Hint

Remember that Svelte needs the variable reference to change to detect updates.

🔧 Debug
advanced
2:00remaining
Why does this Svelte code fail to update the UI when changing an object property?

Look at this Svelte snippet:

  <script>
    let profile = { username: 'user1', status: 'active' };
    function deactivate() {
      profile.status = 'inactive';
      profile = profile;
    }
  </script>

  <button on:click={deactivate}>Deactivate</button>
  <p>Status: {profile.status}</p>

Even though profile = profile; is used, the UI does not update. Why?

Svelte
  <script>
    let profile = { username: 'user1', status: 'active' };
    function deactivate() {
      profile.status = 'inactive';
      profile = profile;
    }
  </script>

  <button on:click={deactivate}>Deactivate</button>
  <p>Status: {profile.status}</p>
ABecause assigning the same object reference does not trigger reactivity in Svelte.
BBecause the status property is read-only and cannot be changed.
CBecause the function deactivate is not called correctly on button click.
DBecause Svelte requires a special method to update nested object properties.
Attempts:
2 left
💡 Hint

Think about how Svelte detects changes to variables and their references.

🧠 Conceptual
expert
2:00remaining
Which statement best explains Svelte's reactivity with arrays and objects?

Choose the most accurate explanation about how Svelte tracks changes in arrays and objects for UI updates.

ASvelte requires using special reactive stores to track changes in arrays and objects.
BSvelte tracks changes by comparing deep equality of arrays and objects on every update.
CSvelte only tracks changes when the variable reference changes; mutating properties or elements directly does not trigger updates.
DSvelte automatically tracks all nested property changes in objects and arrays without extra code.
Attempts:
2 left
💡 Hint

Recall how Svelte's compiler detects changes to variables.