Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1656

How to iterate over an object reactively?

$
0
0

I need to iterate over an object. The object is declared with:

const obj = $state({     one: { checked: true },     two: { checked: false },     three: { checked: true } });

In my Svelte templating code, I do this:

{#each Object.entries(obj) as [key, value]}<div class="mb-8">            {key}: {value.checked}<input type="checkbox" {value} onchange={e => obj[key].checked = e.target.checked}></ion-toggle></div>{/each}

Checking the checkbox does update the value of one of the items of my object, but does not change the value of {value.checked} in the DOM. My suspicion is that obj itself is reactive, but value within obj is not. How do I fix this?

(Note: I'm actually using Ionic and <ion-toggle> instead of <input type="checkbox"> - I know there is a better way to bind the value (bind:value), but this method works throughout the rest of my app for <ion-toggle>.)

I have also attempted making the obj a SvelteMap<string, ClassVersion>, where ClassVersion is:

class MyObj {   checked = $state(false);}

Viewing all articles
Browse latest Browse all 1656

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>