Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I iterate over an array in a Svelte store?

I have an array in store.ts

export let annotations = writable(new Array<Annotation>());

And I'd like to iterate over the array, in component.svelte:

<script lang="ts">
  import { annotations } from '../store';
</script>

<section>
  {#each annotations as highlight, index}
    ...
  {/each}
</section>

This fails with:

Argument of type 'Writable<Annotation[]>' is not assignable to parameter of type 'ArrayLike'

Which makes sense - The Writable object isn't a regular Array, it's a Writable.

How can I iterate over an array in a Svelte store?

like image 639
mikemaccana Avatar asked Dec 22 '25 18:12

mikemaccana


1 Answers

Iteration over the array, in component.svelte:

<script lang="ts">
  import { annotations } from '../store';
</script>

<section>
  {#each $annotations as highlight, index}
    ...
  {/each}
</section>

More info

like image 89
Anatolii Kosorukov Avatar answered Dec 24 '25 06:12

Anatolii Kosorukov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!