Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
How to force render after an If condition in the html tag in svelte

This is what I have in the html of my page :

{#each subCategories as subCategories}
  {#key $cart}
    {($cart.indexOf(subCategories.name) > -1)}
    {#if ($cart.indexOf(subCategories.name) > -1)}
      Test
    {:else}
      <div class="media ali list-group-item list-group-item-action  clique" on:click={() => changeCart(subCategories.name)}>
        <div class="media-left">
            <Management/>
        </div>
        <div class="media-body"  >
            <h4 class="media-heading">{subCategories.name}</h4>
        </div>
      </div>
    {/if}
  {/key}
{/each}

for the js part this is what I have :

import { writable } from "svelte/store";
let cart = writable([]);
function changeCart(subCat) {
    $cart.push(subCat);
    console.log($cart);
}

But the problem is that the if condition don't reload / render another time and the change of case work only after going back to the previous component and going another time to this component.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Because Svelte's reactivity is triggered by assignments, using array methods like push and splice won't automatically cause updates.

Source: https://svelte.dev/tutorial/updating-arrays-and-objects

Solution:

import { writable } from 'svelte/store';
let cart = writable([])

function changeCart(subCat)
{
    // $cart.push(subCat)
    $cart = [...$cart, subCat]
    console.log($cart)
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda