Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rerender {#each array} block in svelte after you push something to an array

Hello I'm making a comment section for my website which works but when I push an object to an array my {#each} block doesn't update here is this block of code

            {#each commentsScript as { userName, rating, comment }, i}
                <div class="flex">
                    <div class="w-1/4">
                        <h5>{userName}</h5>
                        <h5>{rating}</h5>
                    </div>
                    <div class="w-3/4">
                        <p>{comment}</p>
                    </div>
                </div>
            {/each}

In my script section, I push a new comment to an array and it does show up after I refresh the page but I want to rerender it as soon as the user leaves a comment so the user would see hes/her own comment so is there any way I can rerender it?

like image 534
Laycookie Avatar asked Oct 17 '25 17:10

Laycookie


1 Answers

https://svelte.dev/tutorial/updating-arrays-and-objects

You have to assign commentsScript = commentsScript, which would be redundant normally, but that is how svelte works. This is true if you are using push function on array.

like image 65
Simon Harvan Avatar answered Oct 20 '25 06:10

Simon Harvan



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!