Skip to content

Commit dbf635b

Browse files
authored
Merge pull request #727 from sveltejs/gh-706
clear refs in destroy, not unmount, so that refs are populated in ondestroy
2 parents 5c4905a + 4d36908 commit dbf635b

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

src/generators/dom/visitors/Element/Ref.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function visitRef(
1717
`#component.refs.${name} = ${state.parentNode};`
1818
);
1919

20-
block.builders.unmount.addLine(deindent`
20+
block.builders.destroy.addLine(deindent`
2121
if ( #component.refs.${name} === ${state.parentNode} ) #component.refs.${name} = null;
2222
`);
2323

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div ref:element></div>
2+
3+
<script>
4+
export default {
5+
ondestroy() {
6+
this.refOnDestroy = this.refs.element;
7+
}
8+
};
9+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
test(assert, component, target) {
3+
const top = component.refs.top;
4+
const div = target.querySelector('div');
5+
6+
component.set({ visible: false });
7+
assert.equal(top.refOnDestroy, div);
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{#if visible}}
2+
<Top ref:top></Top>
3+
{{/if}}
4+
5+
<script>
6+
import Top from './Top.html';
7+
8+
export default {
9+
data() {
10+
return {
11+
visible: true
12+
};
13+
},
14+
components: {
15+
Top
16+
}
17+
};
18+
</script>

0 commit comments

Comments
 (0)