Skip to content

Commit

Permalink
update keyed each when key expression changes (#5447)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored Sep 24, 2020
1 parent c3b56a1 commit 41d1656
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Fix keyed `{#each}` not reacting to key changing ([#5444](https://github.com/sveltejs/svelte/issues/5444))
* Fix destructuring into store values ([#5449](https://github.com/sveltejs/svelte/issues/5449))
* Fix erroneous `missing-declaration` warning with `use:obj.method` ([#5451](https://github.com/sveltejs/svelte/issues/5451))

Expand Down
5 changes: 5 additions & 0 deletions src/compiler/compile/render_dom/wrappers/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ export default class EachBlockWrapper extends Wrapper {
this.node.expression.dynamic_dependencies().forEach((dependency: string) => {
all_dependencies.add(dependency);
});
if (this.node.key) {
this.node.key.dynamic_dependencies().forEach((dependency: string) => {
all_dependencies.add(dependency);
});
}
this.dependencies = all_dependencies;

if (this.node.key) {
Expand Down
27 changes: 27 additions & 0 deletions test/runtime/samples/each-block-keyed-dyanmic-key/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let count = 0;
let value = 'foo';

export default {
props: {
value() {
count++;
return value;
}
},

html: `
<div>foo</div>
<div>foo</div>
`,

test({ assert, component, target }) {
value = 'bar';
component.id = 1;

assert.equal(count, 4);
assert.htmlEqual(target.innerHTML, `
<div>bar</div>
<div>bar</div>
`);
}
};
8 changes: 8 additions & 0 deletions test/runtime/samples/each-block-keyed-dyanmic-key/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
export let id = 0;
export let value;
</script>

{#each ['foo', 'bar'] as key (id + key)}
<div>{value()}</div>
{/each}

0 comments on commit 41d1656

Please sign in to comment.