Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
Svelte: else if block condition not evaluated after if block condition changes

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:

  1. Set foo to true
  2. Set bar[0] to true
  3. Set foo to false

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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Its not really a bug, its kinda expected behaviour in your scenario, when you changed foo to true then bar to true now bar is set and when changed foo to false again bar didn't change if you think about it to trigger reactivity!

the fix for that is to trigger it yourself once any of the values changes:

{#key foo || bar}
    {#if foo}
        foo!
    {:else if bar.every(x => x)}
        bar!
    {/if}
{/key}

Here is a working repl

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda