DEV Community

Discussion on: Creating your first Svelte5 reactive component

Collapse
 
mfp22 profile image
Mike Pearson

dice could be downstream from rolls

This is how I'd do it in the next version of StateAdapt

const { draw, reset, $state: rolls } = adapt([0], {
  draw: state => [...state, (math random stuff)],
})
const dice = $derived(rolls[rolls.length - 1]);
Enter fullscreen mode Exit fullscreen mode

Just an idea. Probably on dangerous ground with the $state keyword. But it works

Collapse
 
robertobutti profile image
Roberto B.

Thank you for the feedback.
Managing arrays as a state is something that I'm exploring more in order to identify the "best way" to manage them.
For example, i want to replace adding item into an array with :

rolls = [...rolls, dice]
Enter fullscreen mode Exit fullscreen mode

I will monitor the side effects.
I want to explore more "Fine grained reactivity" https://www.youtube.com/watch?v=gGwnF-lxS_Q&t=313s

Collapse
 
robertobutti profile image
Roberto B.

With the new features of Svelte5 and the new reactivity mechanism, it is no longer necessary to reassign the array. An array is "reactive" if it is initialized with $state, and you can use the push method to append elements. (see the draw() function)

Collapse
 
mfp22 profile image
Mike Pearson

For my own future reference svelte-5-preview.vercel.app/#H4sIA...