0

I have problems binding to nested after migrating from svelte4 to svelte5 I have a nested object that i want to manipulate in Svelte JS why do i need to create the Outer? Link to see Svelte Playground

<script>
import { writable } from "svelte/store"
import Toggle from "./Toggle.svelte"
import OuterToggle from "./OuterToggle.svelte"

let condition = $state({name: "app"});

let store = writable({data: {enabled : true}})
</script>
<br>
Not working 
{JSON.stringify($store)}- {$store.data.enabled}
<Toggle bind:data={$store.data}></Toggle><br> // Works in svelte4
<br>
Working <OuterToggle bind:data={$store}></OuterToggle>

the first one has binding that works but the other does not why is that

Toggle:

<script>
let {data = $bindable({enabled:true})} = $props()
function change(e) {
    data.enabled = e.target.checked
}
</script>
<input type="checkbox" checked={data.enabled} onchange={change}>

OuterToggle:

<script>
import Toggle from "./Toggle.svelte"
let {data = $bindable({data :{enabled:true}})} = $props()
</script>
{JSON.stringify(data)}
<Toggle bind:data={data.data}></Toggle>

1 Answer 1

1

This looks like a bug.

Stores have coarse reactivity: When anything in the store is modified, they have to be invalidated; the binding needs to take care of that.

For the OuterToggle instance, this code is generated:

OuterToggle(node_1, {
    get data() {
        $.mark_store_binding();
        return $store();
    },
    set data($$value) {
        $.store_set(store, $$value);
    }
});

For Toggle it's this:

Toggle(node, {
    get data() {
        return $store().data;
    },
    set data($$value) {
        $.store_mutate(store, $.untrack($store).data = $$value, $.untrack($store));
    }
});

The $.mark_store_binding(); missing from the getter in Toggle seems a bit suspicious, unless that is only required when binding directly to the store of course...

In any case, would recommend opening an issue in the Svelte repository if there is none already.


Edit: Adding mark_store_binding to the binding getter appears to fix the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.