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

96
Vistas
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 Respuestas
Responde la pregunta

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 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