mirror of https://github.com/sveltejs/svelte
fix: prevent reactive snippet from reinitializing unnecessarily (#9665)
untrack the invocation itself, only track the snippet function fixes #9652pull/9667/head
parent
5619cd9bfc
commit
83fd001157
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: prevent reactive snippet from reinitializing unnecessarily
|
@ -0,0 +1,62 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `
|
||||
<p>snippet: 0</p>
|
||||
<button>toggle</button>
|
||||
<button>increase count</button>
|
||||
`,
|
||||
props: {
|
||||
get log() {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
|
||||
async test({ assert, target, component }) {
|
||||
const [toggle, increment] = target.querySelectorAll('button');
|
||||
|
||||
await increment?.click();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<p>snippet: 1</p>
|
||||
<button>toggle</button>
|
||||
<button>increase count</button>
|
||||
`
|
||||
);
|
||||
assert.deepEqual(component.log, []);
|
||||
|
||||
await toggle?.click();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<p>component: 1</p>
|
||||
<button>toggle</button>
|
||||
<button>increase count</button>
|
||||
`
|
||||
);
|
||||
assert.deepEqual(component.log, [1]);
|
||||
|
||||
await increment?.click();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<p>component: 2</p>
|
||||
<button>toggle</button>
|
||||
<button>increase count</button>
|
||||
`
|
||||
);
|
||||
assert.deepEqual(component.log, [1]);
|
||||
|
||||
await toggle?.click();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<p>snippet: 2</p>
|
||||
<button>toggle</button>
|
||||
<button>increase count</button>
|
||||
`
|
||||
);
|
||||
assert.deepEqual(component.log, [1]);
|
||||
}
|
||||
});
|
@ -0,0 +1,6 @@
|
||||
<script>
|
||||
let { count, log } = $props();
|
||||
log.push(count);
|
||||
</script>
|
||||
|
||||
<p>component: {count}</p>
|
@ -0,0 +1,22 @@
|
||||
<script>
|
||||
import Inner from "./inner.svelte";
|
||||
|
||||
let { log } = $props();
|
||||
|
||||
let count = $state(0);
|
||||
let show_foo = $state(true);
|
||||
let snippet = $derived(show_foo ? foo : bar);
|
||||
</script>
|
||||
|
||||
{#snippet foo({count})}
|
||||
<p>snippet: {count}</p>
|
||||
{/snippet}
|
||||
|
||||
{#snippet bar(props)}
|
||||
<Inner {...props}></Inner>
|
||||
{/snippet}
|
||||
|
||||
{@render snippet({ count, log })}
|
||||
|
||||
<button onclick={() => show_foo = !show_foo}>toggle</button>
|
||||
<button onclick={() => count++}>increase count</button>
|
Loading…
Reference in new issue