mirror of https://github.com/sveltejs/svelte
feat: make deriveds writable (#15570)
* feat: make deriveds writable * add optimistic UI example * add note to when-not-to-use-effect * add section on deep reactivity * root-relative URL * use hash URL * mention const * make handler async, move into script blockpull/15578/head
parent
2d3b65dfbd
commit
5a8fa69dbf
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
feat: make deriveds writable
|
@ -1,8 +0,0 @@
|
|||||||
import { test } from '../../test';
|
|
||||||
|
|
||||||
export default test({
|
|
||||||
error: {
|
|
||||||
code: 'constant_assignment',
|
|
||||||
message: 'Cannot assign to derived state'
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,5 +0,0 @@
|
|||||||
<script>
|
|
||||||
const a = $state(0);
|
|
||||||
let b = $derived(a);
|
|
||||||
b = 1;
|
|
||||||
</script>
|
|
@ -1,8 +0,0 @@
|
|||||||
import { test } from '../../test';
|
|
||||||
|
|
||||||
export default test({
|
|
||||||
error: {
|
|
||||||
code: 'constant_binding',
|
|
||||||
message: 'Cannot bind to derived state'
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,6 +0,0 @@
|
|||||||
<script>
|
|
||||||
let a = $state(0);
|
|
||||||
let b = $derived({ a });
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<input type="number" bind:value={b} />
|
|
@ -1,8 +0,0 @@
|
|||||||
import { test } from '../../test';
|
|
||||||
|
|
||||||
export default test({
|
|
||||||
error: {
|
|
||||||
code: 'constant_assignment',
|
|
||||||
message: 'Cannot assign to derived state'
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,10 +0,0 @@
|
|||||||
<script>
|
|
||||||
class Counter {
|
|
||||||
count = $state();
|
|
||||||
#doubled = $derived(this.count * 2);
|
|
||||||
|
|
||||||
nope() {
|
|
||||||
this.#doubled += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
@ -1,8 +0,0 @@
|
|||||||
import { test } from '../../test';
|
|
||||||
|
|
||||||
export default test({
|
|
||||||
error: {
|
|
||||||
code: 'constant_assignment',
|
|
||||||
message: 'Cannot assign to derived state'
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,10 +0,0 @@
|
|||||||
<script>
|
|
||||||
class Counter {
|
|
||||||
count = $state();
|
|
||||||
#doubled = $derived(this.count * 2);
|
|
||||||
|
|
||||||
nope() {
|
|
||||||
this.#doubled++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
@ -1,8 +0,0 @@
|
|||||||
import { test } from '../../test';
|
|
||||||
|
|
||||||
export default test({
|
|
||||||
error: {
|
|
||||||
code: 'constant_assignment',
|
|
||||||
message: 'Cannot assign to derived state'
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,5 +0,0 @@
|
|||||||
<script>
|
|
||||||
const a = $state(0);
|
|
||||||
let b = $derived(a);
|
|
||||||
b++;
|
|
||||||
</script>
|
|
@ -0,0 +1,46 @@
|
|||||||
|
import { flushSync } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
html: `
|
||||||
|
<input type="range"><input type="range"><p>0 * 2 = 0</p>
|
||||||
|
`,
|
||||||
|
|
||||||
|
ssrHtml: `
|
||||||
|
<input type="range" value="0"><input type="range" value="0"><p>0 * 2 = 0</p>
|
||||||
|
`,
|
||||||
|
|
||||||
|
test({ assert, target, window }) {
|
||||||
|
const [input1, input2] = target.querySelectorAll('input');
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
input1.value = '10';
|
||||||
|
input1.dispatchEvent(new window.Event('input', { bubbles: true }));
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`<input type="range"><input type="range"><p>10 * 2 = 20</p>`
|
||||||
|
);
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
input2.value = '99';
|
||||||
|
input2.dispatchEvent(new window.Event('input', { bubbles: true }));
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`<input type="range"><input type="range"><p>10 * 2 = 99</p>`
|
||||||
|
);
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
input1.value = '20';
|
||||||
|
input1.dispatchEvent(new window.Event('input', { bubbles: true }));
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`<input type="range"><input type="range"><p>20 * 2 = 40</p>`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,9 @@
|
|||||||
|
<script>
|
||||||
|
let count = $state(0);
|
||||||
|
let double = $derived(count * 2);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<input type="range" bind:value={count} />
|
||||||
|
<input type="range" bind:value={double} />
|
||||||
|
|
||||||
|
<p>{count} * 2 = {double}</p>
|
@ -1,14 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"code": "constant_assignment",
|
|
||||||
"message": "Cannot assign to derived state",
|
|
||||||
"start": {
|
|
||||||
"column": 3,
|
|
||||||
"line": 6
|
|
||||||
},
|
|
||||||
"end": {
|
|
||||||
"column": 29,
|
|
||||||
"line": 6
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
@ -1,9 +0,0 @@
|
|||||||
<script>
|
|
||||||
class Test{
|
|
||||||
der = $derived({ test: 0 });
|
|
||||||
|
|
||||||
set test(v){
|
|
||||||
this["der"] = { test: 45 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
@ -1,14 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"code": "constant_assignment",
|
|
||||||
"message": "Cannot assign to derived state",
|
|
||||||
"start": {
|
|
||||||
"column": 3,
|
|
||||||
"line": 6
|
|
||||||
},
|
|
||||||
"end": {
|
|
||||||
"column": 27,
|
|
||||||
"line": 6
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
@ -1,9 +0,0 @@
|
|||||||
<script>
|
|
||||||
class Test{
|
|
||||||
#der = $derived({ test: 0 });
|
|
||||||
|
|
||||||
set test(v){
|
|
||||||
this.#der = { test: 45 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
@ -1,14 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"code": "constant_assignment",
|
|
||||||
"message": "Cannot assign to derived state",
|
|
||||||
"start": {
|
|
||||||
"column": 3,
|
|
||||||
"line": 6
|
|
||||||
},
|
|
||||||
"end": {
|
|
||||||
"column": 26,
|
|
||||||
"line": 6
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
@ -1,9 +0,0 @@
|
|||||||
<script>
|
|
||||||
class Test{
|
|
||||||
der = $derived({ test: 0 });
|
|
||||||
|
|
||||||
set test(v){
|
|
||||||
this.der = { test: 45 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
Loading…
Reference in new issue