<script>
let count=0;
</script>
{#each list as item, i}
<div class="item">
<Item />
</div>
{/each}
How do I increment the count on some condition inside each block in svelte?
You could use a {@const ...} inside the block like so
<script>
let count=0;
</script>
{#each list as item, i}
{#if somecondition}
{@const count=++count}
<!-- and use it after maybe as Item prop -->
<div class="item">
<Item {count} />
</div>
{#else}
<div class="item">
<Item {count} />
</div>
{/if}
{/each}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With