mirror of https://github.com/sveltejs/svelte
feat: allow usage of getContext() within $derived runes (#13830)
Closes #13493. This PR allows the usage of getContext() inside $derived runes. Previously, you could use it, but only on init and not updates – and this inconsistency was unnecessary. We can make it work just like we do in other places.pull/13842/head
parent
96e2d5a395
commit
d621f59642
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
feat: allow usage of getContext() within $derived runes
|
@ -0,0 +1,13 @@
|
||||
<script>
|
||||
import { getContext } from 'svelte'
|
||||
|
||||
let count = $state(0);
|
||||
let total = $derived(multiply(count));
|
||||
|
||||
function multiply(num) {
|
||||
const context = getContext("key");
|
||||
return num * context;
|
||||
}
|
||||
</script>
|
||||
|
||||
<button onclick={() => count++}>{total}</button>
|
@ -0,0 +1,16 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<button>0</button>`,
|
||||
|
||||
test({ assert, target }) {
|
||||
const btn = target.querySelector('button');
|
||||
|
||||
flushSync(() => {
|
||||
btn?.click();
|
||||
});
|
||||
|
||||
assert.htmlEqual(target.innerHTML, `<button>10</button>`);
|
||||
}
|
||||
});
|
@ -0,0 +1,8 @@
|
||||
<script>
|
||||
import { setContext } from 'svelte'
|
||||
import Child from './Child.svelte'
|
||||
setContext("key", 10)
|
||||
</script>
|
||||
|
||||
<Child />
|
||||
|
Loading…
Reference in new issue