mirror of https://github.com/sveltejs/svelte
feat: allow modifiying derived props (#10080)
It's an unnecessary restruction because it can be worked around (hide it behind a getter/setter), already works for bind:x and prevents valid use casespull/10127/head
parent
b623890439
commit
3624a4c2a0
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
feat: allow modifiying derived props
|
@ -1,5 +1,5 @@
|
||||
<script>
|
||||
const a = $state(0);
|
||||
const b = $derived({ a });
|
||||
b.a += 1;
|
||||
let b = $derived(a);
|
||||
b = 1;
|
||||
</script>
|
||||
|
@ -1,5 +1,5 @@
|
||||
<script>
|
||||
const a = $state(0);
|
||||
const b = $derived({ a });
|
||||
b.a++;
|
||||
let b = $derived(a);
|
||||
b++;
|
||||
</script>
|
||||
|
@ -0,0 +1,15 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `<button>0 / 0</button><button>0 / 0</button>`,
|
||||
|
||||
async test({ assert, target }) {
|
||||
const [btn1, btn2] = target.querySelectorAll('button');
|
||||
|
||||
await btn1?.click();
|
||||
assert.htmlEqual(target.innerHTML, `<button>1 / 2</button><button>1 / 2</button>`);
|
||||
|
||||
await btn2?.click();
|
||||
assert.htmlEqual(target.innerHTML, `<button>2 / 4</button><button>2 / 4</button>`);
|
||||
}
|
||||
});
|
@ -0,0 +1,7 @@
|
||||
<script>
|
||||
let count = $state(0);
|
||||
let double = $derived({ get value() { return count * 2}, set value(c) { count = c / 2 } });
|
||||
</script>
|
||||
|
||||
<button on:click={() => count++}>{count} / {double.value}</button>
|
||||
<button on:click={() => double.value += 2}>{count} / {double.value}</button>
|
Loading…
Reference in new issue