Skip to main content

By default, when you modify the value of an each block, it will add and remove items at the end of the block, and update any values that have changed. That might not be what you want.

It's easier to show why than to explain. Click to expand the Console, then click the 'Remove first thing' button a few times, and notice what happens: it does not remove the first <Thing> component, but rather the last DOM node. Then it updates the name value in the remaining DOM nodes, but not the emoji.

Instead, we'd like to remove only the first <Thing> component and its DOM node, and leave the others unaffected.

To do that, we specify a unique identifier (or "key") for the each block:

{#each things as thing (thing.id)}
	<Thing name={thing.name} />
{/each}

Here, (thing.id) is the key, which tells Svelte how to figure out which DOM node to change when the component updates.

You can use any object as the key, as Svelte uses a Map internally — in other words you could do (thing) instead of (thing.id). Using a string or number is generally safer, however, since it means identity persists without referential equality, for example when updating with fresh data from an API server.

App.svelte
Thing.svelte
<script>
import Thing from './Thing.svelte';

let things = [
{ id: 1, name: 'apple' },
{ id: 2, name: 'banana' },
{ id: 3, name: 'carrot' },
{ id: 4, name: 'doughnut' },
{ id: 5, name: 'egg' }
];

function handleClick() {
things = things.slice(1);
}
</script>

<button on:click={handleClick}> Remove first thing </button>

{#each things as thing}
<Thing name={thing.name} />
{/each}

resolving https://unpkg.com/svelte@4/src/shared/version.js
/* App.svelte generated by Svelte v4.2.19 */
import {
SvelteComponent,
check_outros,
create_component,
destroy_component,
destroy_each,
detach,
element,
empty,
ensure_array_like,
group_outros,
init,
insert,
listen,
mount_component,
safe_not_equal,
space,
transition_in,
transition_out
} from "svelte/internal";

import "svelte/internal/disclose-version";
import Thing from './Thing.svelte';

function get_each_context(ctx, list, i) {
const child_ctx = ctx.slice();
child_ctx[2] = list[i];
return child_ctx;
}

// (19:0) {#each things as thing}
function create_each_block(ctx) {
let thing_1;
let current;
thing_1 = new Thing({ props: { name: /*thing*/ ctx[2].name } });

return {
c() {
create_component(thing_1.$$.fragment);
},
m(target, anchor) {
mount_component(thing_1, target, anchor);
current = true;
},
p(ctx, dirty) {
const thing_1_changes = {};
if (dirty & /*things*/ 1) thing_1_changes.name = /*thing*/ ctx[2].name;
thing_1.$set(thing_1_changes);
},
i(local) {
if (current) return;
transition_in(thing_1.$$.fragment, local);
current = true;
},
o(local) {
transition_out(thing_1.$$.fragment, local);
current = false;
},
d(detaching) {
destroy_component(thing_1, detaching);
}
};
}

function create_fragment(ctx) {
let button;
let t1;
let each_1_anchor;
let current;
let mounted;
let dispose;
let each_value = ensure_array_like(/*things*/ ctx[0]);
let each_blocks = [];

for (let i = 0; i < each_value.length; i += 1) {
each_blocks[i] = create_each_block(get_each_context(ctx, each_value, i));
}

const out = i => transition_out(each_blocks[i], 1, 1, () => {
each_blocks[i] = null;
});

return {
c() {
button = element("button");
button.textContent = "Remove first thing";
t1 = space();

for (let i = 0; i < each_blocks.length; i += 1) {
each_blocks[i].c();
}

each_1_anchor = empty();
},
m(target, anchor) {
insert(target, button, anchor);
insert(target, t1, anchor);

for (let i = 0; i < each_blocks.length; i += 1) {
if (each_blocks[i]) {
each_blocks[i].m(target, anchor);
}
}

insert(target, each_1_anchor, anchor);
current = true;

if (!mounted) {
dispose = listen(button, "click", /*handleClick*/ ctx[1]);
mounted = true;
}
},
p(ctx, [dirty]) {
if (dirty & /*things*/ 1) {
each_value = ensure_array_like(/*things*/ ctx[0]);
let i;

for (i = 0; i < each_value.length; i += 1) {
const child_ctx = get_each_context(ctx, each_value, i);

if (each_blocks[i]) {
each_blocks[i].p(child_ctx, dirty);
transition_in(each_blocks[i], 1);
} else {
each_blocks[i] = create_each_block(child_ctx);
each_blocks[i].c();
transition_in(each_blocks[i], 1);
each_blocks[i].m(each_1_anchor.parentNode, each_1_anchor);
}
}

group_outros();

for (i = each_value.length; i < each_blocks.length; i += 1) {
out(i);
}

check_outros();
}
},
i(local) {
if (current) return;

for (let i = 0; i < each_value.length; i += 1) {
transition_in(each_blocks[i]);
}

current = true;
},
o(local) {
each_blocks = each_blocks.filter(Boolean);

for (let i = 0; i < each_blocks.length; i += 1) {
transition_out(each_blocks[i]);
}

current = false;
},
d(detaching) {
if (detaching) {
detach(button);
detach(t1);
detach(each_1_anchor);
}

destroy_each(each_blocks, detaching);
mounted = false;
dispose();
}
};
}

function instance($$self, $$props, $$invalidate) {
let things = [
{ id: 1, name: 'apple' },
{ id: 2, name: 'banana' },
{ id: 3, name: 'carrot' },
{ id: 4, name: 'doughnut' },
{ id: 5, name: 'egg' }
];

function handleClick() {
$$invalidate(0, things = things.slice(1));
}

return [things, handleClick];
}

class App extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, {});
}
}

export default App;
result = svelte.compile(source, {
generate:
css:
});
/* Add a <style> tag to see compiled CSS */