I have a piece of Svelte code that is supposed to:
Load a number from an external source when clicking on "Load" button
Save the number to an external source when the number changes
https://svelte.dev/repl/5d64d5ba6c5e4df6b12cb4c1fb3b716b?version=3.49.0
<script>
let num
let msgs = []
const inc = function() {
num += 1
}
const load = function() {
const num_ = 1
msgs = [...msgs, 'load num ' + num_]
num = num_
}
const save = function(num_) {
msgs = [...msgs, 'save num ' + num_]
}
$: save(num)
</script>
<button on:click={load}>Load</button>
<button on:click={inc}>Inc</button>
{#each msgs as msg}
<li>{msg}</li>
{/each}
My problem is that, after loading the page and clicking Load and Inc once each, I get this result:
save num undefined
load num 1
save num 1
save num 2
where I have two superfluous saves:
save num undefined
save num 1
I know I can manually track these with additional variables and what not, but is there a better way to avoid these?