in svelte 4, I used the following approach:
<script lang="ts"> class Product { id: number = 0; name: string = ""; description: string = ""; } let products: Product[]=[]; let productForAdd = new Product();</script>}<div><input bind:value={product.name} /><input bind:value={product.description} /><input bind:value={product.image} /><button on:click={() => { products = [...products, productForAdd]; productForAdd = new Product();}> Add</button></div>{#each products as product}<div><div>{product.id}</div><div>{product.name}</div><div>{product.description}</div></div>{/each}
How can I do the same in svelte 5?If I use the Product class without runes, then errors about the lack of reactivity appear.If I add the state rune to a class, I cannot get data from an object created based on this class, $state.snapshot(products) turns out to be empty.And if I add a nested array with other objects to the Product class, for example, of this type:
class Category { name: string = ""; description: string = ""; } class Product { id: number = 0; name: string = ""; description: string = ""; categoryes: Category[]=[]; }
It turns out to be a complete trash