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

93
Visualizações
Is there a way to tell Svelte not to recycle a component?

I don't know if the title is super accurate, couldn't think of a better way to explain it.

Let's say we have a model:

let model = [
  {
    "id": 0,
    "title": "Apple"
  },
  {
    "id": 1,
    "title": "Orange"
  },
  {
    "id": 2,
    "title": "Tomato"
  },
  {
    "id": 3,
    "title": "Avocado"
  }
]

That we render like this:

{#each model as item}
    <Item {...item}></Item>
{/each}

In the Item component we print to console when the component gets mounted and destroyed:

<script>
    import {onMount, onDestroy} from 'svelte'
    
    export let id
    export let title

    onMount(() => {
        console.log('Mounted: ' + title)
    })
    onDestroy(() => {
        console.log('Destroyed: ' + title)
    })

</script>

<div id={id}>{title}</div>

We also have a button, which deletes the "Orange" element from the model:

function deleteOrange() {
    model.splice(1, 1)
    model = model // I don't know if this is the correct way to force an update
}
    
// ...

<input on:click={deleteOrange} type="button" value="Delete Orange">

When the button is pressed, the Orange component disappears from the DOM as it should, but the console says:

"Destroyed: Avocado"

Is there a way to tell Svelte to destroy the "Orange" component, instead of destroying "Avocado" and moving props around to match the model?

Svelte REPL:

https://svelte.dev/repl/f22b37cb88c449118ff76bb5a82c296e?version=3.48.0

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

0

Simply add a (key) to your each loop:

{#each model as item (item.id)}
    <Item {...item}></Item>
{/each}

This will ensure that DOM elements are properly tracked. As per the Svelte documentation:

If a key expression is provided — which must uniquely identify each list item — Svelte will use it to diff the list when data changes, rather than adding or removing items at the end. The key can be any object, but strings and numbers are recommended since they allow identity to persist when the objects themselves change.

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