I don't understand this 1 line in the svelte code

Ignore the comments. Why numbers=numbers is used in the function <script> let numbers = [1, 2, 3, 4]; function addNumber() { // numbers = [...numbers, numbers.length+1]; //numbers[numbers.length] = numbers.length+1 numbers.push(numbers.length + 1); numbers = numbers; } $: sum = numbers.reduce((t, n) => t + n, 0); </script> <p>{numbers.join(' + ')} = {sum}</p> <button on:click={addNumber}> Add a number </button>
2 Replies
JOY
JOYOP2y ago
from what i understand, assigning the same value to a variable causes a refresh in the UI. Without it the button won't do anything. Am i correct?
Jochem
Jochem2y ago
you are correct. The way the reactivity works, numbers.push doesn't trigger an update. By assigning numbers to itself, you trigger that reactivity update which also updates the UI
Want results from more Discord servers?
Add your server