Edit: it is a bug https://github.com/sveltejs/svelte/pull/7043
Considering the following example one would expect 'bar' to be rendered under the second horizontal rule whenever foo is false and bar[0] is true.
<script>
let foo = true
let bar = [false];
</script>
<button on:click={() => foo = !foo}>
Toggle foo
</button>
<button on:click={() => bar[0] = !bar[0]}>
Toggle bar
</button>
<hr>
{@html `foo: ${foo}, bar: ${bar.every(x => x)}`}
<hr>
{#if foo}
foo!
{:else if bar.every(x => x)}
bar!
{/if}
However, if the following steps are executed, the reactivity seemingly breaks:
- Set
foototrue - Set
bar[0]totrue - Set
footofalse
Nothing is rendered under the horizontal rule even though the else if block condition is satisfied. This is not the case if the else if condition is simply bar[0], so it seems like using a function is the cause for it not being evaluated. Similar behavior when bar is true before foo is toggled and false after foo is toggled again, unexpectedly 'bar' is rendered at the bottom.
The same effect can be observed when foo is switched between null and [] with the else if condition being Array.isArray(bar).
Is this a bug or am I missing something?
else ifdoesn't seem to be reeveluated when the opening#ifcondition changes. I can't tell if/why there's a difference between a method call likebar.every(x => x)and the pointer to a value likebar[0], but my solution would be to use areactive statementfor the method call$: condition = bar.every(x => x)and use the variable down below{:else if condition}which seems to work as expected REPL