mirror of https://github.com/sveltejs/svelte
fix: handle deep assignments to `$state()` class properties correctly (#10289)
fixes #10276pull/10291/head
parent
c8da99646a
commit
107ec1c848
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"svelte": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: handle deep assignments to `$state()` class properties correctly
|
@ -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 / 0</button><button>1 / 0</button>`);
|
||||||
|
|
||||||
|
await btn2?.click();
|
||||||
|
assert.htmlEqual(target.innerHTML, `<button>2 / 1</button><button>2 / 1</button>`);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,24 @@
|
|||||||
|
<script>
|
||||||
|
class Counter {
|
||||||
|
container = $state({ count: -1 });
|
||||||
|
#private = $state({ count: -1 });
|
||||||
|
|
||||||
|
constructor(initial_count) {
|
||||||
|
this.container.count = initial_count;
|
||||||
|
this.#private.count = initial_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
increment() {
|
||||||
|
this.container.count += 1;
|
||||||
|
this.#private.count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
get private_count() {
|
||||||
|
return this.#private.count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const counter = new Counter(0);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button on:click={() => counter.container.count++}>{counter.container.count} / {counter.private_count}</button>
|
||||||
|
<button on:click={() => counter.increment()}>{counter.container.count} / {counter.private_count}</button>
|
Loading…
Reference in new issue